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

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.

So far, I found these two methods that seem to work:

Method 1, changing superclass instance's __class__ to subclass:

Quote:
>>> x = superclass()
>>> x.__class__ = subclass
>>> x.method()  # this executes method defined in subclass

Method 2, creating a subclass instance, and change its __dict__ to
reference superclass instance's:

Quote:
>>> x = superclass()
>>> y = subclass ()
>>> y.__dict__ = x.__dict__
>>> y.method () # since y.__dict__ refers to x's data, x's data gets

changed by method

Are these two methods totally equivalent?  Are there any gotcha's?  Is
there a more elegant way?

Note, the following did not work, resulting with the TypeError below:

Quote:
>>> x = superclass()
>>> subclass.method (x)

TypeError: unbound method must be called with class instance 1st
argument

(The error message is somewhat misleading.)

By the way, I am using python 1.5.2, Mac version.

- Rick Lee



Fri, 14 Mar 2003 09:52:26 GMT  
 Invoking a subclass's method on its superclass's instance

Quote:

> 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.

Quote:
> So far, I found these two methods that seem to work:

> Method 1, changing superclass instance's __class__ to subclass:

> >>> x = superclass()
> >>> x.__class__ = subclass
> >>> x.method()  # this executes method defined in subclass

This method changes the class of x into subclass.  This is a dangerous
feature, but can be useful for mode-switching operations.

Quote:
> Method 2, creating a subclass instance, and change its __dict__ to
> reference superclass instance's:

> >>> x = superclass()
> >>> y = subclass ()
> >>> y.__dict__ = x.__dict__
> >>> y.method () # since y.__dict__ refers to x's data, x's data gets
> changed by method

> Are these two methods totally equivalent?  Are there any gotcha's?  Is
> there a more elegant way?

This causes x and y to share the same dictionary.  I guess this works
but it is fairly obscure and fragile.

Quote:
> Note, the following did not work, resulting with the TypeError below:

> >>> x = superclass()
> >>> subclass.method (x)

> TypeError: unbound method must be called with class instance 1st
> argument

> (The error message is somewhat misleading.)

Yes, a little misleading.  It is complaining because x is not an
instance of subclass.  A more verbose error message might have made it
clearer, but I'm not sure: "unbound method must be called with
instance of class defining method (or its superclass) as first
argument."

-- Jeremy Hylton <http://www.python.org/~jeremy/>



Fri, 14 Mar 2003 03:00:00 GMT  
 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

Quote:


> > 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/>



Fri, 14 Mar 2003 03:00:00 GMT  
 Invoking a subclass's method on its superclass's instance
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)

Quote:
-----Original Message-----


Sent: Monday, September 25, 2000 2:48 PM

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



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



Fri, 14 Mar 2003 03:00:00 GMT  
 Invoking a subclass's method on its superclass's instance
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.

Quote:

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


> Sent: Monday, September 25, 2000 2:48 PM

> 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



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



Sat, 15 Mar 2003 08:25:30 GMT  
 Invoking a subclass's method on its superclass's instance

Quote:

> 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.

I would try something like:

===============================================================================
module A.py:
-------------------------------------------------------------------------------
class Reference :
    def behavior (self) :
        # implements reference behavior

module B.py:
-------------------------------------------------------------------------------
def behavior_variant_B (self) :
    # implements some specific behavior variant

module Test_Driver.py
-------------------------------------------------------------------------------
from A import Reference
from B import behavior_variant_B

object = Reference ()
# do some tests

object.behavior = behavior_variant_B # switch to behavior variant B
# do some more tests

del object.behavior                  # switch back to reference behavior
===============================================================================

You can repeat that with as many behavior variants as necessary. There
is no need for subclasses implementing the variants.

--

Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92



Sat, 15 Mar 2003 03:00:00 GMT  
 Invoking a subclass's method on its superclass's instance
Rick:

This is a fairly complex requirement, which I suspect you've abbreviated
somewhat to help us retain our sanity.  Therefore, I may be completely off
beam...

Quote:

> 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.

Had you though of using attributes, and setting them to unbound methods
or plain functions as appropriate?  The real methods could then just call
the attributes.

Quote:
> 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.

I.e. you would change the appropriate attribute of that instance to the
function exhibiting the correct "pathological" behavior.

Quote:
> - 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.

So you want to repeat a sequence of tests with a particular method iteratinf
over a defined set of functions?

Quote:
> - 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.

Perhaps you could define a method to put objects back into a "non-pathological"
state between tests (unless you want the to retain their current pathologies).

Quote:
> - There will be literally hundreds of behaviour variants for each method of
> each class.

Sounds ugly.  I take it complete test coverage isn't a goal ...

Quote:
> - 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).

So you want external code to access the guts of objects whose class is
defined in another module?  Python at least allows you to do this, and
of course there's nothing wrong with subclassing classes defined in
modules.

Quote:
> - 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.

So you *do* need a "restore" method.  Or possibly a "restore" method for
each method?

Quote:
> - Quite often a behaviour variant is an extension to the reference behaviour;
> so I want to make this easy to do.

One solution might use lists of functions to represent the set of
mathods you want to concatenate, and then

        for f in functionlist:
                apply(f, args)

for example?  That way you can "add extra behavior" by appending another
function to the list.

Quote:
> - I must be able to introduce a new behaviour variant(s) while the test system
> is running.

Well, this argues for attributes, I suspect.  You really don't want to go
changing instance methods willy nilly.

Quote:
> - 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.

As long as you don't revise class definitions too radically between runs you
should be able to pickle your state and restire it later.

Quote:
> 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.

This does indeed seem natural.  That's precisely what object-oriented
programming is intended to facilitate.  Is the problem that you want your
subclass instances to revert back to being superclass instances?  In that
case, perhaps your __init__ method should check for a subclass instance
and copy state from the subclass instance.  That way, you could morph your
pathological variants back to the superclass as required.

Quote:
>              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.

If you only want to set the subclass' methods to those of a superclass to
make your "pathological variants" revert back to standard behaviour, would
it not be better to simply re-create standard objects, copying whatever
state you needed to retain?

Quote:

> > What exactly is the higher-level problem you are trying to solve?

[ ... ]

It's almost as though the whole problem sapce representation should become
one huge data structure, which you use to drive your simulation.

Hope this helps.  If not, feel free to ignore it.

regards
 Steve
--
Helping people meet their information needs with training and technology.



Sat, 15 Mar 2003 03:00:00 GMT  
 Invoking a subclass's method on its superclass's instance
Another potential solution that would solve my problem is to copy all the data
attributes of one class instance to another (in this case from a superclass instance
to subclass instance, and then in the reverse direction later on).  Is this as simple
as the following?
Quote:
>>> import copy
>>> subclass.__dict__ = copy.deepcopy(superclass.__dict__)



Sun, 16 Mar 2003 03:00:00 GMT  
 Invoking a subclass's method on its superclass's instance
Oops, I should have said:

Quote:
>>> subclassInstance.__dict__ = copy.deepcopy(superclassInstance.__dict__)

to make clear my intensions.
Quote:

> Another potential solution that would solve my problem is to copy all the data
> attributes of one class instance to another (in this case from a superclass instance
> to subclass instance, and then in the reverse direction later on).  Is this as simple
> as the following?

> >>> import copy
> >>> subclass.__dict__ = copy.deepcopy(superclass.__dict__)



Sun, 16 Mar 2003 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Using methods in the superclass' superclass

2. How to find a subclass' instance variables

3. calling superclass' method with list positional arg

4. Variable 'superclass'es?

5. Why can't instance methods be pickled?

6. I'm sorry, name of instance inside its methods

7. looking up an instance's methods

8. instance creations methods for VW's Image class

9. 'variable subclass', OrderedCollectionNoDuplicates, SortedCollectionNoDuplicates

10. 'variable subclass', OrderedCollectionNoDuplicates, SortedCollectionNoDuplicates

11. A bug in '.button invoke'?

12. How to copy a subclass from a superclass?

 

 
Powered by phpBB® Forum Software