Can one class have multiple metaclasses?

Alex Martelli aleax at mail.comcast.net
Sun Jan 15 22:26:35 EST 2006


David Hirschfield <davidh at ilm.com> wrote:

> Is it possible for one class definition to have multiple metaclasses?
> 
> I don't think it is, but I'm just checking to make sure.

You're right, it isn't.

>  From what I know, you can only define "__metaclass__" to set the 
> metaclass for a class, and that's it, is that correct?

Yes, it's correct.  Just as an instance is an instance of one (leafmost)
class, so a class is an instance of one (leafmost) metaclass -- if you
want multiple classes or metaclasses, you must inherit.

The last recipe in the 2nd ed of Python Cookbook shows how to design an
automatic "metaclass conflict reconciler" to do the inheritance on your
behalf, btw.


Alex



More information about the Python-list mailing list