[Python-ideas] __iter__ implies __contains__?

Masklinn masklinn at masklinn.net
Sun Oct 2 17:39:00 CEST 2011


On 2011-10-02, at 16:10 , Steven D'Aprano wrote:
> 
> If you have a class that you don't want to be iterable but otherwise obeys the iteration protocol, that is easy to fix: have __iter__ raise TypeError. An easy fix for unusual and trivial problem.
Yes but you have to know it exists in the first place, and it is not obvious that `in` without `__contains__` will use `__iter__`, let alone that `iter()` without `__iter__` will use `__getitem__` (and I submit as my case study in not being obvious that *Antoine* was surprised by this behavior).


More information about the Python-ideas mailing list