I have a metaclass:
class MyMeta(type):
def __init__(cls, name, bases, dct):
# Do something
...
return super(MyMeta, cls).__init__(cls, name, bases, dct)
and a class:
class MyClass(object):
__metaclass__ = MyMeta
When I use these I get the following error:
TypeError: Error when calling the metaclass bases
type.__init__() takes 1 or 3 arguments
What’s the problem, and why does type.__init__() take a precisely variable number of arguments?
The problem is that in the upgrade from python 2.5 to python 2.6
type.__init__()was changed so that you are no longer required to pass incls. So simply make thesupercall:Another solution is to avoid the
supercall altogether and do this (although it’s a little less nice):And everything will work fine (in python >= 2.6).
As to why
type.__init__()can take differing numbers of arguments, check out the documentation. It’s so that as well as using it as a constructor, you can calltype(myobject)and it will return the type ofmyobject:See What is a metaclass in Python? for more information on metaclasses and type.