Overriding a method in an instance
Martyn Quick
mrq at for.mat.bham.ac.uk
Wed May 2 09:36:32 EDT 2001
On Wed, 2 May 2001, Kalle Svensson wrote:
> Sez Martyn Quick:
> > I was wondering what the correct way to change the definition of a method
> > of an instance of a class is. The following seems to work:
>
> Well, that depends on what you mean by correct. IMHO, the *correct* way
> would be to create a subclass (or use delegation):
>
> I'm not sure about your situation, of course. Perhaps you can present a
> compelling reason to change methods on instances without subclassing (or
> delegation)?
OK, the precise situation is that I'm creating two groups of widgets
(each of which is defined as a class which subclasses from
Pmw.MegaWidget) and that I need a way to get them to talk to each
other. I give the first class a dummy function which I was intending to
redefine to get info from the second class instance. The situation can be
summarized as follows:
class FirstClass:
...
def func(self):
pass
class SecondClass:
...
one = FirstClass()
two = SecondClass()
def do_stuff():
[get info from two and use it in one]
one.func = do_stuff
[Hope that makes some sort of sense.]
Any thoughts?
Martyn
--------------------------------------------------------
Dr. Martyn Quick (Research Fellow in Pure Mathematics)
University of Birmingham, Edgbaston, Birmingham, UK.
http://www.mat.bham.ac.uk/M.R.Quick
More information about the Python-list
mailing list