【问题标题】:TypeError: Error when calling the metaclass bases a new-style class can't have only classic basesTypeError:调用元类基时出错,新式类不能只有经典基
【发布时间】:2012-03-29 10:45:39
【问题描述】:

类的集合定义为:

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 的子类,有人可以告诉我这里出了什么问题以及我可以如何改变我正在做的事情来消除错误吗?

【问题讨论】:

    标签: python oop typeerror new-style-class


    【解决方案1】:

    问题是线

    class A():
    

    应该是:

    class A(object):
    

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

    【讨论】:

      【解决方案2】:

      就我而言,我尝试降低软件包的版本。成功了!

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

      【讨论】:

        猜你喜欢
        • 2016-07-03
        • 2018-08-23
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-04
        • 1970-01-01
        相关资源
        最近更新 更多