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

Christian Tanzer tanzer at swing.co.at
Tue Sep 26 02:22:08 EDT 2000


rwklee at home.com wrote:

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

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list