[Python-Dev] misbehaving __contains__

Steven Bethard steven.bethard at gmail.com
Wed Jan 23 02:03:57 CET 2008


On Jan 22, 2008 5:40 PM, Raymond Hettinger <python at rcn.com> wrote:
> [Daniel Stutzbach]
> > There are many places in the C implementation where a slot
> > returns an int rather than a PyObject.  There other replies
> > in this thread seem to support altering the signature of the
> > slot to return a PyObject. Is this setting a precedent that
> > _all_ slots should return a PyObject?
>
> I hope not.
[snip]
> And, would we lose the nice relationship expressed by:
>
>     for elem in container:
>         assert elem in container

We've already lost this if anyone really wants to break it::

    >>> class C(object):
    ...     def __iter__(self):
    ...         return iter(xrange(3))
    ...     def __contains__(self, item):
    ...         return False
    ...
    >>> c = C()
    >>> for item in c:
    ...     print item in c
    ...
    False
    False
    False

Of course, anyone who decides to break their container classes in that
way is asking for trouble. ;-)

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list