Invoking a subclass's method on its superclass's instance
dbrueck at edgix.com
Mon Sep 25 23:17:30 CEST 2000
What exactly is the higher-level problem you are trying to solve?
I understand what you are trying to implement, but not why. It may be that
the best solution has _nothing_ to do with dynamically overloading methods,
so we need to know what the bigger picture is all about. (Give us some
context for the problem, but from a higher level than the implementation
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Lee, Rick
Sent: Monday, September 25, 2000 2:48 PM
To: python-list at python.org
Subject: Re: Invoking a subclass's method on its superclass's instance
This is what I am trying to accomplish:
Dynamically during run time, being able to change the behaviour of an
existing instance, by substituting the implementation of a method of that
instance with another implementation; and then some time later, to undo this
change. Note that this substitution is for one particular instance only;
other existing instances are not to be affected. Also, a changed
implementation is often an extension to the original behaviour, so it would
be nice to be able to invoke the superclass's method to do the original
work, and then execute the extension of the behaviour.
I hope this clarifies my design goal. Any advice for a more robust and
"kosher" solution to the above would be much appreciated.
You said: "wouldn't it be easier to organize the classes so that the method
is defined in the superclass,
possibly with hooks for overriding or specialization in the subclass." I
don't particularly want to put these hooks into a superclass, because I need
to have this capability, and therefore hooks, in pretty well every class and
every method that I am writing.
Jeremy Hylton wrote:
> Rick Lee <rwklee at home.com> writes:
> > I like to be able to invoke a subclass's method on an instance of its
> > superclass, also this method makes changes to the instance's data.
> This is a strange request. If the method is defined in a subclass,
> presumably it depends on state or behavior that is also defined in
> that subclass. What if the instance you are trying to invoke the
> method on doesn't have the same state/behavior?
> If you are designing the class hierarchy, wouldn't it be easier to
> organize the classes so that the method is defined in the superclass,
> possibly with hooks for overriding or specialization in the subclass.
> Perhaps you could explain what you are trying to accomplish in a bit
> more detail and you could get better advice. The feature's you are
> proposing to use are useful for introspection, but I would be wary of
> any package that uses them for its normal operation.
> -- Jeremy Hylton <http://www.python.org/~jeremy/>
More information about the Python-list