[Python-ideas] __iter__ implies __contains__?

Steven D'Aprano steve at pearwood.info
Wed Oct 5 01:38:25 CEST 2011


Greg Ewing wrote:
> Raymond Hettinger wrote:
> 
>> Really?   Passing around iterators is a basic design pattern
>> http://en.wikipedia.org/wiki/Iterator_pattern for lots of languages.
> 
> I'm not suggesting that we stop using iterators altogether,
> only that reiterables are often preferable when there's
> a choice. Passing a reiterable to a piece of third-party
> library code is safer and more future-proof than passing an
> iterator, because it makes less assumptions about what will
> be done to it.

I don't think it is up to the supplier of the data to try to guess what 
the code will do. After all, there is no limit to what silly things a 
called function *might* try. Why single out "iterate over an iterator 
twice" for special consideration?

To put it another way, if a function is advertised as working on 
iterables (either implicitly or explicitly), I would have no compunction 
about passing a finite iterator and expecting it to work. If it fails to 
work, the bug is in the function, not my code for using an iterator.

(Infinite iterators are a special case... I wouldn't expect to be able 
to use them in general, if for no other reason than most algorithms 
expect to terminate.)


-- 
Steven



More information about the Python-ideas mailing list