[Python-ideas] __iter__ implies __contains__?

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Oct 3 09:27:39 CEST 2011


Terry Reedy wrote:
> 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.

The trick will be getting people to recognise when they're
requiring a reiterable, and to bother making this check
when they are.

Needing to perform any kind of type check on one's arguments
is LBYL-ish and not very Pythonic.

-- 
Greg



More information about the Python-ideas mailing list