Question about subclassing and overriding methods

Frank Millman frank at chagford.com
Thu Sep 7 04:33:30 EDT 2006


Bruno Desthuilliers wrote:
> Frank Millman wrote:
>
> This "replacement" happens at instance initialisation time - ie, after
> the class object have been created. If you don't want this to happen,
> either skip the call to Test.__init__ in Test2.__init__, or make this
> call with False as second param, or redefine getx2 in Test2. Or go for a
> saner design...
>

Thanks, Bruno, you gave me the clue to a less ugly workaround.

In my particular case, when I do subclass Test, y is always True.
Therefore I can rewrite it like this -

class Test2(Test):
    def __init__(self,x):
        Test.__init__(self,x,True)
    def getx2(self):
        print x*3

As you suggested, I redefine getx2 instead of getx, and it works as I
want.

Slightly less insane, I hope ;-)

Frank




More information about the Python-list mailing list