Invoking a subclass's method on its superclass's instance

Lee, Rick rickylee at americasm01.nt.com
Mon Sep 25 16:48:25 EDT 2000


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.

- Rick

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 mailing list