[Tutor] Overriding a method in a class

Alan Gauld alan.gauld at btinternet.com
Sat May 14 10:12:35 CEST 2011


"Terry Carroll" <carroll at tjc.com> wrote in message

>I have a pretty basic point of confusion that I'm hoping I can have 
>explained to me.  I have a class in which I want to override a 
>method, and have my method defined externally to the class definition 
>invoked instead.

Is there any reason you can'tt override in the uisual way by 
inheritance?

It seems to me you are changing the behaviour of a class which
means its now a different kind of thing. So it deserves to be a
new class - a LowerThing or whatever.

> ### example, continued
> def addlower(self, s2):
>     self.stuff.append(s2.lower()) # add it as lower case
>
> B = Thing()
> B.add_the_stuff=addlower

But this doesn't add a method it adds a reference to a function.
methods are not simply functions defined inside a class.
methods need to be bound to the class for the self "magic"
to happen.

[There was a good post by Steven a few weeks ago
that explained the difference. It might be worth searching out]

HTH,

Alan G. 




More information about the Tutor mailing list