[Python-ideas] __iter__ implies __contains__?

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


On 10/2/2011 4:25 PM, Guido van Rossum wrote:
> On Sun, Oct 2, 2011 at 12:57 PM, Antoine Pitrou<solipsis at pitrou.net>  wrote:
>> Hello?
>> The issue is not that walking over an iterator changes the state of the
>> iterator, it is that "x in A" iterates over A at all.
>
> Hello?
> That is the original definition of "in".

I had the same reaction as Guido. Iteration is the *only* generic way to 
tell if an item is in a sequence or other collection . The direct hash 
access of sets and dicts is exceptional. The direct calculation for 
range is a different exception. For the other builtin sequences, and for 
typical iterators, which lack the information for an O(1) shortcut, 'in' 
(and .__contains__ if present) has to be iteration based.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list