简体   繁体   中英

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

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM