[ZODB-Dev] Strange behaviour of __dict__ and dir on Persistent derived classes

Tim Peters tim at zope.com
Thu Jul 22 10:47:53 EDT 2004


[Syver Enstad]
> Using ZODB 3.2
>
> NB: I am talking about class objects not instance objects.
>
> cls.__dict__ and dir(cls) doesn't return the methods defined in base
> classes.

I can't imagine why you might expect cls.__dict__ to do so.  If you do
expect that, get over it <wink>.  cls.__dict__ generally won't contain
inherited methods (unless cls overrides them).

> I guess that this is property of ExensionClass and that the behaviour
> will be equal to "regular" python class objects when I upgrade to 3.3?

In 3.3, Persistent *is* a (new-style) Python class, so yes wrt dir().
Here's an example under 3.3:

>>> from persistent import Persistent
>>> class A:
...    def amethod(self): pass
...
>>> class B(Persistent, A):
...    def bmethod(self): pass
...
>>> 'bmethod' in dir(B)
True
>>> 'amethod' in dir(B)
True
>>>

But the inherited amethod still isn't in B's __dict__:

>>> 'amethod' in B.__dict__
False
>>> 'bmethod' in B.__dict__
True
>>>

> Is there an easy way to enumerate the methods a class actually has (both
> defined directly and from base classes) under ZODB 3.2?

Sorry, I don't know enough about ExtensionClass.  I suppose you could write
a function to crawl over cls.__bases__ recursively, but that's not notably
easy (e.g., Python's implementation of dir() consumes > 200 lines of C).

Anyone else?



More information about the ZODB-Dev mailing list