Question about subclassing and overriding methods
Frank Millman
frank at chagford.com
Fri Sep 8 01:45:55 EDT 2006
Dennis Lee Bieber wrote:
> On 7 Sep 2006 01:33:30 -0700, "Frank Millman" <frank at chagford.com>
> declaimed the following in comp.lang.python:
>
> > 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 ;-)
> >
>
> I do hope this is just a simplified example <G>
>
> Otherwise I'd have implemented it the other way around -- don't
> compute the value on each get, but rather on the initialization
>
Thanks for the reply, Dennis. In fact, my real requirement has got
nothing to do with computing a value. That was just a way of
illustrating my need to call a different version of a method depending
on certain factors.
I have since come up with a different approach, which I have explained
in a new thread. It is still a bit complicated, so I would still
appreciate any comments on how to simplify it.
Frank
More information about the Python-list
mailing list