11

类的集合定义为:

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')

运行时,出现此错误:

TypeError: Error when calling the metaclass bases
    a new-style class can't have only classic bases

我需要元类(我认为)在我的代码中具有 B 的已知字符串表示。这样做的原因是无关紧要的,但它将极大地帮助未来的更新。

因此,假设我需要 C 成为 B 的元类,而 B 将成为 A 的子类,有人可以告诉我这里出了什么问题以及如何更改我正在做的事情以消除错误吗?

4

2 回答 2

18

问题是线路

class A():

它应该是:

class A(object):

这样,你就可以让 A 成为一个新的样式类。空括号没有任何意义,而且,我仍然在 stackoverflow 和任何地方看到它们。为什么,哦,为什么?

于 2012-03-13T01:37:12.267 回答
0

就我而言,我尝试降低软件包的版本。有效 !

第二次试用,由于版本问题,这在 python 2 中存在。将您的代码升级到 python3。有用。

于 2021-09-06T13:54:10.877 回答