[Python-ideas] Generator unpacking

Paul Moore p.f.moore at gmail.com
Mon Feb 15 03:49:33 EST 2016


On 15 February 2016 at 08:39, Andrew Barnert <abarnert at yahoo.com> wrote:
>> is very subtle, and (even worse) only significant if value is an
>> iterable as opposed to a concrete container such as a list.
>
> You mean iterator, not iterable. And being "concrete" has nothing to do with it--a dict view, a memoryview, a NumPy slice, etc. aren't iterators any more than a list is.

Precisely :-) Nor is a range object, see the (other!) mistake I made.
>
> This is exactly why I think we need an official term like "collection" for "iterables that are not iterators" (or "iterables whose __iter__ doesn't return self" or similar). People struggling to come up with terms end up confusing themselves--not just about wording, but about actual concepts. As proven below:

Indeed.

>> (And of course in my first attempt I forgot I needed iter(range(...))
>> as range is not a pure iterable - proving my point about the subtle
>> semantics!)
>
> Ranges are as iterable as both lists and iterators. You're a smart guy, and you know Python. So why do you make this mistake? Because you don't have a term to fit "range" into, so your brain struggles between two prototypes--is it like a list, or like a generator? Well, it's lazy, so it's like a generator, so you don't need to call iter here, right? Nope.

Thanks for providing another perspective on my point. This whole area
is rather too full of semantic confusion (I'm not as sure as you seem
to be that agreeing on a terminology will address that issue, but
that's a side matter) and I think we should be very careful about
introducing syntax that requires a good understanding of the concepts
to use correctly, when there's a perfectly acceptable explicit form
(even if it is slightly more verbose/repetitive).

Paul


More information about the Python-ideas mailing list