[Python-ideas] __iter__ implies __contains__?

Terry Reedy tjreedy at udel.edu
Mon Oct 3 05:20:30 CEST 2011


On 10/2/2011 4:23 PM, Oleg Broytman wrote:

>> No. That is implied by the iterator protocol
>
>     This is exactly the issue that is being discussed. In my very humble
> opinion classes that produce non-restartable iterators should not allow
> containment tests to use iterators.

I think this is backwards.

Functions that take a generic iterable as input should call iter() on 
the input *once* and use to iterate just once, which means not using 'in'.

Functions that need a re-iterable should check for the presence of 
.__next__. That will exclude file objects and other iterables.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list