Need advice on subclassing code

Rusty Shackleford rs at natchie.mine.nu
Tue Nov 15 10:25:49 EST 2005


Hi --

We have some code that returns an object of a different class, depending
on some parameters.  For example:

if param x is 1 and y is 1, we make an object of class C_1_1.
if param x is 1 and y is 2, we make an object of class C_1_2.

C_1_1 and C_1_2 share a common C ancestor, and in practice may be
identical, but theoretically, could have the same function name with two
different implementations underneath.

We have a file where all the C_X_Y classes are defined.  It looks sort
of like this:

class C_1_1(C):
    """Creates a x=1 and y=1 class"""
    def __init__(self):
        C.__init__(self, 1, 1)

class C_1_2(C):
    """Creates a x=1 and y=2 class"""
    def __init__(self):
        C.__init__(self, 1, 2)

99% of the specific classes do the exact same thing.  For a tiny few,
the class definition looks like this:

class C_3_5(C):
    """Creates a x=3, y=5 class."""
    def __init__(self):
        C.__init__(self, 3, 5)
    
    def foo(self):
        """Redefine the default C.foo() function."""
        return 99

The reason for this is that we want to allow different classes to do
non-standard behavior.  In practice, however, it turns out that most of
the time, we don't need anything special.

Is this the best solution?  Is there some way of doing a default vs.
non-default deal, without having to manually hardcode all the different
possible subclasses?

Thanks for the help.

-- 
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles



More information about the Python-list mailing list