[ZODB-Dev] Persistent breaks __contains__?

Tim Peters tim at zope.com
Mon Feb 9 19:25:45 EST 2004


[William]
> Hi.  I'm new to ZODB, and the following baffles me.
>
>         Python 2.3.3 (#2, Jan 13 2004, 00:47:05)
>         [GCC 3.3.3 20040110 (prerelease) (Debian)] on linux2
>         Type "help", "copyright", "credits" or "license" for more
>                    information.
>         >>> import ZODB
>         >>> class Foo:
>         ...     def __contains__(self, key):
>         ...             return True
>         ...
>         >>> class Bar(ZODB.Persistent):
>         ...     def __contains__(self, key):
>         ...             return True
>         ...
>         >>> 2 in Foo()
>         True
>         >>> 2 in Bar()
>         Traceback (most recent call last):
>           File "<stdin>", line 1, in ?
>         AttributeError: __getitem__
>         >>> Bar().__contains__(2)
>         True
>
> Why isn't Bar.__contains__ called by "2 in Bar()"?

Versions of ZODB based on ExtensionClass generally don't play well with
Python class features introduced after ExtensionClass was written; the
__contains__ protocol is one of those.

ZODB 3.3 will replace Persistent with a work-alike "new-style" Python class,
and your example works as intended under that:

>>> import ZODB
>>> ZODB.__version__
'3.3a2'
>>> from Persistence import Persistent
>>> class Bar(Persistent):
...     def __contains__(self, key):
...         return True
...
>>> 2 in Bar()
True
>>>




More information about the ZODB-Dev mailing list