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

主宰稳场 提交于 2019-12-06 19:58:12

问题


A collection of classes defined as:

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

When run, gives this error:

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

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.

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?


回答1:


The problem is the line

class A():

It should be:

class A(object):

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?



来源:https://stackoverflow.com/questions/9677163/typeerror-error-when-calling-the-metaclass-bases-a-new-style-class-cant-have-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!