rut*_*ord 10 python oop typeerror new-style-class
类的集合定义为:
class A():
@staticmethod
def call():
print('a')
class C(type):
def __repr__(self):
return 'somename'
class B(A):
__metaclass__ = C
@staticmethod
def call():
print('b')
def boundcall(self):
print('bound')
Run Code Online (Sandbox Code Playgroud)
运行时,会出现此错误:
TypeError: Error when calling the metaclass bases
a new-style class can't have only classic bases
Run Code Online (Sandbox Code Playgroud)
我需要元类(我认为)在我的代码中有一个已知的B字符串表示.有这个原因的原因不是重点,但它对未来的更新有很大帮助.
因此,假设我需要C作为B的元类,B将是A的子类,有人可以告诉我这里出了什么问题,以及我如何改变我正在做的删除错误的方法?
pil*_*her 18
问题是这条线
class A():
Run Code Online (Sandbox Code Playgroud)
它应该是:
class A(object):
Run Code Online (Sandbox Code Playgroud)
这样,你就成了一个新的风格类.空的parens没有任何意义,而且,我仍然在stackoverflow和各处看到它们.为什么,为什么?