I would need to think more about this. I'm tempted not to do this, and
let these ABCs denote the *explicit* presence of __contains__ and
__iter__, respectively. Something that's iterable but doesn't
implement __contains__ supports the 'in' operator very inefficiently
(through linear search) which we might not want to encourage.
--Guido
On Feb 8, 2008 5:35 PM, Raymond Hettinger <[EMAIL PROTECTED]> wrote:
> I'm wondering if collections.Iterable should inherit from
> collections.Container"?
>
> In Python, anything that supports __iter__ automatically supports tests using
> the "in" operator:
>
> class A:
> def __iter__(self):
> for i in (1,2,3):
> yield i
> >>> 2 in A()
> True
>
> The two concepts are deeply related. Anywhere we can write "for x in s: ..."
> we can also write "if x in s: ...".
>
> The new definition of Iterable would look like this:
>
> class Iterable(Container):
> __metaclass__ = ABCMeta
>
> @abstractmethod
> def __iter__(self):
> while False:
> yield None
>
> @classmethod
> def __subclasshook__(cls, C):
> if cls is Iterable:
> if any("__iter__" in B.__dict__ for B in C.__mro__):
> return True
> return NotImplemented
>
> def __contains__(self, value):
> for v in self:
> if v == value:
> return True
> return False
>
>
> Raymond
> _______________________________________________
> Python-3000 mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe:
> http://mail.python.org/mailman/options/python-3000/guido%40python.org
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe:
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com