Determining if a superclass is being initialized from a derived class

Roland Schlenker rol9999 at attglobal.net
Sat Jan 20 20:38:44 EST 2001


Timothy Grant wrote:
> 
> Hi folk,
> 
> I have a superclass that I'm using for a project. The superclass has a
> debugging function that is called whenever the command line option --debug
> is used.
> 
> I have run into a situation where I need to subclass the superclass. I want
> access to the debugging information, but not until after the subclass is
> fully initialized.
> 
> class superclass:
>         def __init__(self):
>                 self.a = 1
>                 self.b = 2
>                 if self.debug:
>                         self.dodebug()
>         def dodebug():
>                 print self.a
>                 print self.b
> 
> class subclass(superclass):
>         def __init__(self):
>                 superclass.__init__(self)
> 
>                 self.a = 2
>                 self.b = 4
> 
>                 if self.debug:
>                         self.dodebug()
> 
> In the above, dodebug() is called twice. I would like to only call it the
> second time.

DEBUG = 1

class superclass:
    def __init__(self):
        self.debug = DEBUG
        self.a = 1
        self.b = 2
        if self.debug:
            self.dodebug()

    def dodebug(self):
        print self.a
        print self.b

class subclass(superclass):
    def __init__(self):
        superclass.__init__(self)
        self.debug = DEBUG
        self.a = 2
        self.b = 4

        if self.debug:
            superclass.dodebug(self)

    def dodebug(self):
        pass

Will this work for you?

Roland Schlenker



More information about the Python-list mailing list