metaclasses: timestamping instances

Michele Simionato michele.simionato at gmail.com
Sat Sep 1 18:44:11 CEST 2007


On Sep 1, 6:07 pm, Steve Holden <st... at holdenweb.com> wrote:
<snip>
>
> Debugging with Wing IDE and examining the classes at a breakpoint shows
> this to be true (even after Y's __metaclass__ assignment is commented out):
>
>  >>> X.__metaclass__
> <class '__main__.Meta'>
>  >>> Y.__metaclass__
> <class '__main__.Meta'>
>  >>>

For the benefit of the readers I will just point out that in order
to determine the metaclass of a class it is far better NOT to relay on
the
__metaclass__ attribute. The right thing to to is to look at the
__class__
attribute, or to use type. Here is a (made up) example
where .__metaclass__ gives
the wrong result:


In [9]: class M(type): pass
   ...:

In [10]: class B: __metaclass__ = M
   ....:

In [11]: B.__metaclass__ = None # now the hook is set to None, but the
metaclass does not change

In [12]: B.__class__
Out[12]: <class '__main__.M'>

       Michele Simionato




More information about the Python-list mailing list