Comparison with Ruby ?

Jeremy Hylton jeremy at alum.mit.edu
Thu Feb 22 22:10:48 EST 2001


On the issue of super and overriding: I'd like to recommend again the
use of explicit renaming to make this a little clearer.  It's an idea
I first saw in MzScheme --
    http://www.cs.rice.edu/CS/PLT/packages/mzscheme/index.html
-- and it has a very natural mapping to Python.

class Foo(Bar, Baz):
    __super_meth1 = Bar.meth1
    __super_meth2 = Baz.meth2

    def meth1(self, arg):
        self.__super_meth1(arg)

    def meth2(self, arg):
        self.__super_meth2(arg)

The idea is to create new bindings for the method objects in the
current class.  Then invocation of the method in the parent class
looks like a normal method call.  It's important to add the __ first.
If you don't and more than one class in the inheritance hierarchy
overrides the same method, you'll be in trouble.  It's merely
convention to make it __super, but it helps to indicate what is going
on.

A nice side-effect of this style is that it localizes the information
about what dependencies there are on parent classes.  If you change
the base classes of Foo, you've got all the information about specific
methods of base classes that are being called -- as opposed to have
Baz.meth2(self, arg) scattered throughout the body of Foo.

Jeremy





More information about the Python-list mailing list