TypeError:在调用元类基类时,新类不能仅具有经典基类时出错 [英] TypeError: Error when calling the metaclass bases a new-style class can't have only classic bases

查看:138
本文介绍了TypeError:在调用元类基类时,新类不能仅具有经典基类时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一组定义为的类:

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的已知字符串表示形式.这样做的原因是无关紧要的,但是它将对将来的更新有很大的帮助.

I need the metaclass (I think) to have a known string representation of B in my code. Reason for having that is beside the point but it'll greatly help with future updates.

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

So assuming I need C to be the metaclass of B and B will be a subclass of A can someone tell me what is going wrong here and how I might change what I'm doing to remove the error?

推荐答案

问题出在那行

class A():

应该是:

class A(object):

这样,您将A设置为新样式类.空的parens毫无意义,而且,我仍然在stackoverflow和任何地方都看到它们.为什么,为什么?

That way, you make A a new style class. The empty parens make no sense whatsoever, and still, I keep seeing them on stackoverflow and everywhere. Why, oh why?

这篇关于TypeError:在调用元类基类时,新类不能仅具有经典基类时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆