I'm trying to understand how it works, but I can't do it.
>>> l = (1, 2, 3)
>>> a, b, c = l # a = 1; b = 2; c = 3
Every element assigned to variable. But if amount unpacking variables not equal size of iterable object, we will catch exception.
>>> l = ('first', 'second')
>>> a, b, c = l
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
The same exception happened when variables not enough
>>> l = ('first', 'second', 3)
>>> a, b = l
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
But if I want create function to unpacking any size iterable object, that can I do? Something like this:
>>> l = ('first', 'second')
>>> a, b, c = unpacking(l) # a = 'first'; b = 'second'; c = None
How iterable unpacking working under the hood? When iterator stopped? How to understand the variables over? How define how many objects I must return?