[英]TypeError: __init_subclass__() takes no keyword arguments
I'm trying to create a metaclass but when I assign it to another class I receive the error TypeError: __init_subclass__() takes no keyword arguments
but I don't implement any __init_subclass__
.我正在尝试创建一个元类,但是当我将它分配给另一个 class 时,我收到错误TypeError: __init_subclass__() takes no keyword arguments
但我没有实现任何__init_subclass__
。 Why is this function being called?为什么会调用这个 function?
class Meta(type):
def __new__(cls, name, bases, dct):
return super().__new__(cls, name, bases, dct)
class MyClass(meta=Meta):
pass
Change meta
to metaclass
.将meta
更改为metaclass
。 Any keyword arguments passed to the signature of your class are passed to its parent's __init_subclass__
method.任何传递给 class 签名的关键字 arguments 都会传递给其父级的__init_subclass__
方法。 Since you entered meta
instead of metaclass
this meta
kwarg is passed to its parent's ( object
) __init_subclass__
method:由于您输入了meta
而不是metaclass
这个meta
kwarg 被传递给它的父( object
) __init_subclass__
方法:
>>> object.__init_subclass__(meta=5)
TypeError: __init_subclass__() takes no keyword arguments
A similar error would be raised if you actually implemented a __init_subclass__
but made a typo:如果您实际实现了__init_subclass__
但打错了字,则会引发类似的错误:
class Parent:
def __init_subclass__(cls, handler=None):
super().__init_subclass__()
cls.handler = handler
class CorrectChild(Parent, handler=5):
pass
class TypoChild(Parent, handle=5):
# TypeError: __init_subclass__() got an unexpected keyword argument 'handle'
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.