The problem is that the cls
argument (which is the metaclass object) is not passed on when you call type
, therefore the class object Y
that is created and returned does not have any reference to the metaclass Z
.
If you replace the last line in __new__
with
return super(Z, cls).__new__(cls, name, bases, attrs)
then it works. Note that even though cls
is used in super
we still have to provide cls
as an argument as well, since super
here returns an unbound method (see here for more).
As an alternative to using super one could use:
return type.__new__(cls, name, bases, attrs)
The important thing is that we give cls
(our metaclass object Z
) to the classmethod __new__
. The shorter form type(name, bases, attrs)
fills in type
itself for the cls
argument, which is of course wrong. This error is similar to calling an instance method with the wrong self
argument.
I prefer using super
, since this is better style.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…