[Python-ideas] iterable.__unpack__ method

Steven D'Aprano steve at pearwood.info
Tue Feb 26 01:07:25 CET 2013


On 26/02/13 10:30, Greg Ewing wrote:
> Devin Jeanpierre wrote:
>> On Mon, Feb 25, 2013 at 7:45 AM, Jan Kaliszewski <zuo at chopin.edu.pl> wrote:
>>>    a, b, *() = iterable
>>
>> Of course, you can't unpack anything into (), because Python never had
>> that syntax, but you get the idea.
>
> -1, this is just as arbitrary as ... or a lone *.
>
> I prefer ... myself, because visually it has a low profile
> and doesn't draw undue attention to something that you're
> saying you don't care about.
>
> Maybe ... could be blessed as an official "don't care"
> assignment target, like _ in Haskell. That would make this
> usage less of a special case (although it would still be
> partially special, since it suppresses unpacking of the
> rest of the iterator).

Please no. _ is already overloaded too much. In i18n contexts, _ is
conventionally used as the function for getting display text. In the
interactive interpreter, _ is used for the last result. And in source
code, _ is often used by convention as a "don't care" variable.

Turning _ into a magic "stop unpacking" symbol would break code that
uses it as a "don't care" target when unpacking:

spam, _, _, ham, eggs = stuff

and would be ambiguous when there is an underscore as the right-most
target. Would it mean, unpack but I don't care about it, or don't
unpack?

I'm still not seeing why this is important enough to add magic syntax.
If you want to unpack the first five values from an iterator, without
exhausting it, we already have some good solutions:

spam, ham, eggs, cheese, tomato = (next(it) for i in range(5))

or

spam, ham, eggs, cheese, tomato = itertools.islice(it, 5)


Neither of these are so confusing to read or onerous to use as to
justify new magic syntax:


spam, ham, eggs, cheese, tomato, $$$$ = it  # for some value of $$$




-- 
Steven



More information about the Python-ideas mailing list