extending methods ?

Robert Brewer fumanchu at amor.org
Wed Oct 1 13:01:03 EDT 2003


Arnd Baecker wrote:

>  a) Invoke the superclass' method  in which all the
>     relevant variables needed for the method in class_B are defined
>     as self.<variable_name>. And then they can be
>     accessed in class_B.

There is a fourth option:

class A:
    def __init__(self, x):
        y = x + 42
        self.postinit(y)

    def postinit(self, y):
        pass

class B(A):
    def postinit(self, y):
        dosomethingspecialwith(y)

class C(A):
    def postinit(self, y):
        dosomethingelsewith(y)

This is similar to a) but doesn't "invoke a bunch of code" if you don't
want it to. You don't even have to define postinit() in class A if you
don't want to; you could just skip the call if your (sub)class doesn't
have that method/attribute. It breaks the isolation of A a bit, but if
that's an integral part of your design, I won't take issue with it. ;)


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list