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

blippy picture blippy · Mar 13, 2012 · Viewed 23.1k times · Source

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?

Answer

pillmuncher picture pillmuncher · Mar 13, 2012

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?