Metaclass error: type .__ init __ () takes 1 or 3 arguments

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 class:

class MyClass(object):
    __metaclass__ = MyMeta

When I use them, I get the following error:

E       TypeError: Error when calling the metaclass bases
E           type.__init__() takes 1 or 3 arguments

What is the problem and why does it type.__init__()take the exact number of arguments?

+5
source share
1 answer

The problem is that when upgrading from python 2.5 to python 2.6 type.__init__()it was changed so that you no longer have to go through cls. So just call the call super:

return super(MyMeta, cls).__init__(name, bases, dct)

Another solution is to avoid the call superas a whole and do it (although this is a little less nice):

return type.__init__(cls, name, bases, dct)

And everything will work fine (in python> = 2.6).

, type.__init__() , . , , , type(myobject) myobject:

>>> number = 1
>>> type(number)
<type 'int'>
>>> type('my string')
<type 'str'>

. Python? .

+8

All Articles