[Python-Dev] py3k: TypeError: object.__init__() takes no parameters

Alexandre Passos alexandre.tp at gmail.com
Fri Jan 16 18:04:12 CET 2009


On Fri, Jan 16, 2009 at 2:12 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>
> I do not understand.  You know it is going to run the .__init__ of its one
> and only base class, which here is object.

Because this class might be used as base of another class. Take this
trivial example code (in py2.6):

class A(object):
    def __init__(self, a):
        #super(A, self).__init__(a)
        self.a = a
        print "A"

class B(object):
    def __init__(self, a):
        #super(B, self).__init__(a)
        self.b = a
        print "B"

class C(A, B):
    def __init__(self, a):
        super(C, self).__init__(a)
        self.c = a
        print "C", dir(self)

C(1)


Running the last line shows that A's constructor got called, but not
B's constructor. The only way to make sure all __init__s are called in
this example is by doing



class A(object):
    def __init__(self, a):
        super(A, self).__init__(a)
        self.a = a
        print "A"

class B(object):
    def __init__(self, a):
        #super(B, self).__init__(a)
        self.b = a
        print "B"

class C(A, B):
    def __init__(self, a):
        super(C, self).__init__(a)
        self.c = a
        print "C", dir(self)

C(1)

which is really ugly (as in, why is B's call to super.__init__
commented but not A's, if A and B are otherwise identical?)

I'm not sure, but I think the proper behavior for object.__init__
should be ignoring all args.
-- 
 - Alexandre


More information about the Python-Dev mailing list