__init__ concerns

Peter Wang pzw1 at hotmail.com
Mon Dec 10 23:36:50 EST 2001


i'm subclassing UserDict to journal transactions to gdbm, and i'm
writing my own __init__() to take an open journal handle.

it occurs to me that since i don't have *any idea* what the base
class's constructor needs or does, i should probably call it, in order
to ensure that my object is valid.  then it occurs to me that i don't
know what the UserDict constructor looks like in its entirety, but it
must be callable with no arguments, since i can do "x =
UserDict.UserDict()".  so the problem is kind of solved: i can just
call UserDict.__init__(self).

however, the bigger question of properly calling parent classes'
constructors still remains.  i figured you can always put a line into
the subclass's constructor:

def __init__(self, *args):
    apply(BASECLASS.__init__, (self,) + args)

but i thought this was kind of a kludge.  what is the proper python
idiom (or, failing that, the proper technique) for ensuring that a
subclass instance is properly initialized, especially when details of
the base class are unknown?

TIA,
peter



More information about the Python-list mailing list