[Python-ideas] __iter__ implies __contains__?

Guido van Rossum guido at python.org
Mon Oct 3 06:04:04 CEST 2011


On Sun, Oct 2, 2011 at 8:54 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 10/2/2011 6:00 PM, Greg Ewing wrote:
>
>> It was probably a mistake not to make a clearer distinction
>> between iterables and iterators back when the iterator
>> protocol was designed, but we're stuck with it now.
>
> It is extremely useful that iterators are iterables.

And I can assure you that this was no coincidence, mistake or
accident. It was done very deliberately.

> The distinction needed
> between iterators and reiterable non-iterators is easy:

> def reiterable(iterable):
>  return hasattr(iterable, '__iter__') and not hasattr(iterable, '__next__')
>
> In a context where one is going to iterate (and call __iter__ if present)
> more than once, only the second check is needed. Functions that need a
> reiterable can make that check at the start to avoid a possibly obscure
> message attendant on failure of reiteration.

Unfortunately most people must aren't going to learn rules like this.
(Gee, even experienced Python programmers can't explain the
relationship between __eq__ and __hash__ properly.) This is where ABCs
would shine if they were used more pervasively -- you'd just assert
that you had a Sequence (or a Collection or whatever) rather than
having to make obscure hasattr checks for __dunder__ names. (And those
hasattr checks aren't infallible. E.g. a class that defines __iter__
but not __next__ for its instances would incorrectly be accepted.)

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list