[Python-ideas] __iter__ implies __contains__?

Nick Coghlan ncoghlan at gmail.com
Sun Oct 2 22:37:32 CEST 2011


On Sun, Oct 2, 2011 at 4:23 PM, Oleg Broytman <phd at phdru.name> wrote:
>   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.

And that is the part that should probably be explicitly called out as
advice in PEP 8 (and perhaps in the docs themselves): iterators (as
opposed to iterables) should likely override __contains__ to raise
TypeError, and non-container iterables (like IO objects) should likely
also be set to raise TypeError if containment tests are not
well-defined for the type.

Whether we adopt that advice in the standard library will need to be
judged on a case by case basis, since it *would* be a breach of
backwards compatibility and thus may require a DeprecationWarning
period.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list