super and __init__

Noah noah at noah.org
Fri Sep 8 14:52:57 EDT 2006


Am I the only one that finds the super function to be confusing?

I have a base class that inherits from object.
In other words new style class:

class foo (object):
    def __init__ (self, arg_A, arg_B):
        self.a = arg_A
        self.b = arg_B
        #   Do I need to call __init__ on "object" base class?

class bar (foo):
    def __init__ (self, arg_Z):
        self.z = "Z" + arg_Z
        foo.__init__(self, 'A', arg_Z)    #  this is the old-style
class way
    def __str__ (self):
        return self.a + self.b + self.z

I don't know how people will use the "bar" class
in terms of inheritance. I don't want to lock anyone
out of multiple inheritance, but I'd like to  have to
worry about it as little as possible. From what I've
read using  the  old style of calling the
base class __init__ can cause conflicts
if the class is later part of a diamond relationship.

I just want "bar" to initialize the properties that it add
to the base class and to have it tell the base class to
initialize the inherited properties. The old way seemed
clear enough; although, a bit ugly. The super function
seems like it would make this more clear, but
it doesn't (to me).

Is there a "just do this" answer for 90% of the use cases?

Yours,
Noah




More information about the Python-list mailing list