Metaclasses broke in 2.2?

Guido van Rossum guido at
Tue Aug 14 21:06:44 CEST 2001

> From: Drew Csillag <drew_csillag at>

> Another question is: will you be able to inherit from C and get the same
> behavior as the current situation where ideally this would work:
> class meta:
>     def __init__(self, klass, bases, dict):
>         pass
> class foo:
>     __metaclass__ = meta
> print 'foo is', foo  # foo *is* an meta instance
> class bar(foo): pass # dies here

This dies because your meta is a classic class.  If you make it a
new-style class by inheriting from object, it can be made to work by
defining a __new__ method on the meta class.

But normally you would inherit meta from type, wouldn't you?

> print 'bar is', bar  # would be lovely if bar was an instance of meta

Yes, this works.

Here's a small example that works in current CVS:

    class meta(object):
	def __new__(cls, name, bases, dict):
	    return object.__new__(cls)
	def __init__(self, name, bases, dict):, self.bases, self.dict = name, bases, dict
	def __repr__(self):
	    return "<meta instance %s>" %

    class foo:
	__metaclass__ = meta

    print foo

    class bar(foo):

    print bar

> In this case, foo and bar are type objects, not instances of meta,
> which is what I'm looking to do.  Is this part of what you were saying
> about it not working yet, or has my head exploded without my knowledge
> and I'm overlooking the obvious?

Neither.  What's not working yet is that in certain cases overriding
__new__ causes an infinite loop, and I need to fix it. :-(

> I guess what I'm really trying to say is that for a library I
> currently have using 2.1, currently the user can be totally ignorant
> of the fact that they are not defining regular classes, but
> metaclasses, and ideally, I'd like to keep it that way.

If you can smuggle a "__metaclass__ = type" into their namespace, you
can get the same effect.

--Guido van Rossum (home page:

More information about the Python-list mailing list