[Python-ideas] iterable.__unpack__ method

Jan Kaliszewski zuo at chopin.edu.pl
Tue Feb 26 01:13:30 CET 2013


26.02.2013 00:37, Greg Ewing wrote:

> João Bernardo wrote:
>> Python already supports this odd syntax
>> a, b, *[] = iterable

Indeed, I didn't know that. :-|

>> because it interprets the [] not as an empty list, but as an empty 
>> "list of identifiers". Maybe it could be used for something useful.
>
> No, because it already has a meaning: there must be no more
> values left in the sequence.
>
>> BTW, the del syntax has the same "problem"
>> del a, b, (c,), [d], []
>
> Or just
>
>    [] = iterable
>
> The surprising thing is that a special case seems to be
> made for ():
>
>>>> () = []
>   File "<stdin>", line 1
> SyntaxError: can't assign to ()
>
> It's surprising because () and [] are otherwise completely
> interchangeable for unpacking purposes.

Not entirely...

     >>> a, b, [c, d] = 1, 2, (3, 4)
     >>> a, b, (c, d) = 1, 2, (3, 4)

OK.

     >>> a, b, *[c, d] = 1, 2, 3, 4
     >>> a, b, *(c, d) = 1, 2, 3, 4

OK as well -- *but*:

     >>> a, b, [] = 1, 2, []   # it's ok
     >>> a, b, () = 1, 2, ()   # but it's not
       File "<stdin>", line 1
     SyntaxError: can't assign to ()

...and:

     >>> a, b, *[] = 1, 2    # it's ok
     >>> a, b, *() = 1, 2    # but it's not
       File "<stdin>", line 1
     SyntaxError: can't assign to ()

Strange... (inb4: http://www.youtube.com/watch?v=5IgB8XKR23c#t=118s ).

*j




More information about the Python-ideas mailing list