[Python-ideas] iterable.__unpack__ method
João Bernardo
jbvsmo at gmail.com
Tue Feb 26 02:20:40 CET 2013
2013/2/25 Greg Ewing <greg.ewing at canterbury.ac.nz>
> João Bernardo wrote:
>
>> Python already supports this odd syntax
>>
>> a, b, *[] = iterable
>>
>> 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.
>
>
Why have two things with the same meaning?
a, b = iterable
a, b, *[] = iterable
Both are the same... The *[] thing is about 100% useless right now.
And the empty "list" syntax is so not used it was possible to segfault
pypy<https://bugs.pypy.org/issue1364>
by putting that in a for loop.
>
> 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
That's what I'm saying... [] and () during unpaking aren't the same thing
when they normally should...
The same happens with the "del" syntax!
One can think about this to see if an iterable is empty and raise an error
if it is not:
[] = iterable
But
*[], = iterable
Has the exact same meaning and is very cryptic.
BTW:
This is the code golf winner to make a list of chars:
*s, = 'string'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130225/667de0fe/attachment.html>
More information about the Python-ideas
mailing list