[Python-ideas] Consider making enumerate a sequence if its argument is a sequence

Akira Li 4kir4.1i at gmail.com
Fri Oct 2 03:59:15 CEST 2015


Andrew Barnert via Python-ideas
<python-ideas at python.org> writes:

> On Sep 30, 2015, at 22:39, Akira Li <4kir4.1i at gmail.com> wrote:
... 
>> Use *iterable* instead of "non-iterator iterable" -- it is that simple.
>
> No it isn't. The word "iterable" just means "iterable". When you want
> to talk about sequences—a subtype of iterables—you don't just say
> "iterable", you say "sequence"...

"iterables that aren't iterators" unlike sequences do not introduce new
requirements -- __iter__ returning non-self doesn't count -- the return
value is still an _arbitrary_ iterator e.g., it may return the same
iterator each time. Sequences on the other hand do
introduce new requirements (__len__, __getitem__ and their specific
semantics that distinguishes them from Mappings).

> ... And likewise, when you want to talk about iterables that aren't
> iterators, or iterables that are repeatable, or any other subtype of
> iterables, you have to use a word (or phrase) that actually means what
> you're saying.

I have no objection to the phrase "repeatable iterable" because it does
introduce a useful distinction.

...
>>  if you need an iterator; use the term *iterator* --
>>  otherwise use *iterable* unless you need something more specific e.g.,
>>  *seq* name is common for generic sequences
>
> Why would you use "seq" instead of "sequence" for the name of the abstract sequence type?

I meant in the code e.g., random.choice(seq), to communicate that an
arbitrary iterable is not enough.

> And, more importantly, what name do you use when you need something
> more specific than "iterable", but less specific than "sequence"—as in
> the glossary entry for dict views, for example?

As I said, I would use the term "dict views". If you mean how "dict
views" could be defined then I've already linked more than once to the
corresponding Python docs issue with the patch
http://bugs.python.org/issue25286

>> I don't remember ever using "non-iterator iterable".
>
> Why would you expect to remember using it, when you're replying to a
> message where I invented it for lack of an established better name
> (and in hopes that someone would come up with one)?
>
>> "non-iterator
>> iterable" does not qualify as more specific. You need to introduce new
>> requirements to the type for that.
>
> There are things that are iterables, that are not non-iterator
> iterables, but the reverse is not true. It's a name for a strict
> subset. Which means it's more specific.

Could you provide a non-hypothetical practical example from existing
code of a function that accepts arbitrary iterables but rejects
iterators?

Perhaps discussing a specific example would help to iron out the
terminology. 



More information about the Python-ideas mailing list