Possible bug in "metaclass resolution order" ?

Michele Simionato michele.simionato at gmail.com
Sun Sep 18 03:39:31 EDT 2005


Pedro Werneck wrote:

>>> class M_A(type): pass
...


>>> class A: __metaclass__ = M_A
...
>>> class M_B(M_A): pass
...
>>> class B(A): __metaclass__ = M_B
...
>>> class C(B): __metaclass__ = M_A
...
>>> C.__class__


<class '__main__.M_B'>

>>> C.__metaclass__


<class '__main__.M_A'>


> Is this supposed to happen ?

Yes, or at least I feel this is a reasonable behavior. You get the
stricted metaclass(M_B) and not the more generic one
(M_A), so you don't lose anything. Remember that given a class C, its
metaclass is given by C.__class__, not by C.__metaclass__,
despite the name. I wrote some code to automatically solve metaclass
conflicts (see for instance my Oxford lectures
http://www.phyast.pitt.edu/~micheles/oxford-lectures.zip) and it works
in the same way, it uses the strictest metaclass.
You argue that in this case an error should be raised, since "errors
should never pass silently". May be. You are free to post
the bug report and look at the opinions of the developers. I am happy
enough with the current behavior and I would just
update the docs.

        Michele Simionato


          Michele Simionato




More information about the Python-list mailing list