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

Rick Lee rwklee at home.com
Mon Sep 25 20:25:30 EDT 2000


I am trying to implement a simulator, ie. a simulated entity.  The model of the
entity being simulated maps very well to a class structure, with approx. 10-20
classes, each with approx. 10 methods.  The simulated entity will be used to
test a product under development, ie., as far as the product under development
is concerned, it can't tell the difference between the simulated entity and the
real entity through the defined interface.  In order to test this product under
development well, I would like to introduce unexpected behaviour and behaviour
variants into the simulated entity, and see whether the product under test
would react gracefully or properly.

Here are the major characteristics of this test system, germane to this
discussion:

- When the test system is running, I like to be able to select a particular
behaviour variant for a particular class instance, in order to perform a
particular test.
- To allow for flexibility for complexity, I would like to be able to select a
set of particular behaviour variants, each for a particular class instance,
during each test.  To allow for this, I would restrict a behaviour variant as
one that involves only one class method.
- In one test session, there will be many tests performed one after another.
If things go well, I don't want to have to restart the test system between
tests.
- There will be literally hundreds of behaviour variants for each method of
each class.
- They must reside in different modules, because they will be written and used
by different testers; these testers will actually be users of this simulator,
(and beginner Python coders).
- This gives rise to the concept of a reference behaviour: it is the default
behaviour for each class method.  After each test, the system is quickly reset
to its reference (default) behaviour set with all the class instances remain
intact, and then another subset of behaviour variants is chosen for the next
test.
- Quite often a behaviour variant is an extension to the reference behaviour;
so I want to make this easy to do.
- I must be able to introduce a new behaviour variant(s) while the test system
is running.
- I must be able to save the simulated entity's state to a file; and reuse it
to restart the test system some time later.  The state space is pretty complex;
but it maps well to a class structure.

I thought a natural and elegant solution is to implement only the default
behaviour in the reference model of class methods, and then implement each
behaviour variant as a method of a subclass with the same method name as its
superclass's.  As the test system starts, only the reference model starts up
(in some test scenarios that is all that is needed.)  To begin a test, the
tester will introduce his behaviour variants, and tell the test system to
switch to them for a selected set of instances.  After the test, he tells the
system to switch back to the reference behaviour; and then repeat for a
different test.


Dave Brueck wrote:

> 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
> perspective)
>
> -----Original Message-----
> 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.
>
> - 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/>
>
> --
> http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list