0

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?

prokoptsev
  • 149
  • 1
  • 9
  • 2
    Why do you believe that you need this? – Ignacio Vazquez-Abrams Jun 05 '15 at 15:42
  • 2
    I can't think of any situation where that would be useful. Could you explain what it is that you are trying to do that would require that functionality? Perhaps we could provide an alternate way of solving the problem at hand. – Jake Griffin Jun 05 '15 at 15:43
  • 2
    There's no way I can think of to do this without *also* passing the number of targets to `unpacking`, which makes it slightly less neat. – jonrsharpe Jun 05 '15 at 15:44
  • 1
    If there are less variables than items, you can do `a, *b = ('first', 'second', 3)`. `b` will be `['second', 3]` then. But setting extra variables to `None` is probably not easily possible. – Kolmar Jun 05 '15 at 15:46
  • @Kolmar It's just in poython 3 – Mazdak Jun 05 '15 at 15:48
  • 1
    Check http://stackoverflow.com/a/5333816/1860929. I think its a direct dup. – Anshul Goyal Jun 05 '15 at 15:51
  • also if you just silently fill up the missing variables with `None` i think that is the point where it's not really "pythonic" anymore but dangerous and exceptionally hard to debug – Jörn Hees Jun 05 '15 at 15:55
  • I don't think the dupe is related but not what the OP is asking – Padraic Cunningham Jun 05 '15 at 15:58
  • @prokoptsev, if you pass then length of the assignments you can give default values http://pastebin.com/xQgb3A6X – Padraic Cunningham Jun 05 '15 at 16:03
  • No idea why it got closed, it's not an exact duplicate, but anyway here is a very dirty way to do something similar with `inspect`: http://pastebin.com/LcEs3sXc – Kolmar Jun 05 '15 at 16:27
  • @IgnacioVazquez-Abrams why not, if I want understand how it work? I guess my english not well as I think, because I feel my question not was understood. – prokoptsev Jun 08 '15 at 12:57
  • @PadraicCunningham I thought about it, but in this way I must know in advance the amount of input parameters. Thx. – prokoptsev Jun 08 '15 at 13:08
  • @JakeGriffin The question arose when I was parsing the string into firstName, lastName and middleName. Some string not contain middleName. At this moment I thought about how unpacking work? Of course I could see the source code, but I not good in C. And I think maybe on SO I can will find a simple intuitive answer. – prokoptsev Jun 08 '15 at 13:31

0 Answers0