Metaclass of a metaclass
Peter Otten
__peter__ at web.de
Tue Jun 5 05:32:16 EDT 2012
Steven D'Aprano wrote:
> I was playing around with metaclasses and I wondered what would happen if
> the metaclass itself had a metaclass. Sort of a metametaclass.
>
> Apparently it gives an error. Can anyone explain why this does not work?
>
> # Python 3.2
>
>
>
>>>> class MyType(type): # A metaclass...
> ... def __repr__(self):
> ... s = super().__repr__()
> ... return s.replace('class', 'metaclass')
> ...
>>>> class Meta(metaclass=MyType): # ... of a metaclass.
> ... pass
> ...
>>>> Meta
> <metaclass '__main__.Meta'>
>>>>
>>>> isinstance(Meta, type)
> True
I think you want isinstance(Meta(), type), and this returns False.
>>>> class MyClass(metaclass=Meta): # And now try to use it.
> ... pass
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: object.__new__() takes no parameters
>
>
>
> What am I doing wrong?
class A(metaclass=M):
pass
is equivalent to
A = M(name, bases, classdict)
and as the error message suggests B needs a compatible signature.
More information about the Python-list
mailing list