"inherited" keyword in Python?

Alex Martelli aleaxit at yahoo.com
Mon Dec 4 10:58:18 EST 2000


"Carsten Gaebler" <cg at schlund.de> wrote in message
news:3A2B9EFA.3A64C13B at schlund.de...
> Hi there!
>
> Does Python know something like the "inherited" keyword from Borland
> Pascal? In Pascal it is used to call a method of a class's superclass,
> i.e. instead of saying "superclass.method()" you say "inherited
> method()". Can be quite helpful if you change the name of the superclass
> or just forgot it. :-)

Python has multiple inheritance, so there is (in general) no single
'superclass' that you could use that way.


When this was last discussed in this forum (regarding a different
language for comparison -- Java or Smalltalk, I believe -- anyway,
some other single-inheritance language which uses 'super' as the
keyword), the solution that eventually emerged was (quite similar
to what Stroustrup proposes in C++ in "Design and Evolution"):

class SomeClass(ABaseClass,AnotherOne,YetAnother):
    __super = AnotherOne
    # etc, etc
    def somemethod(self, foo, bar):
        print "before"
        __super.somemethod(self, foo, bar)
        print "after"

The key idea is that a class must explicitly choose one of its
superclasses as 'the' super (presumably, in a derived-from-
single-inheritance style, the others are just mixins, rather
than having any deep semantic significance), and bind its
classobject to the __super class-variable (the two leading
underlines, without _trailing_ underlines, are important: they
tell Python that this is an identifier specific to this one
class, and avoid clashes with other classes in the inheritance
graph that may be using the same idea).


The choice is clearly simplest for a class which has but one
superclass, but it still needs to be explicit:

class SomeOtherClass(AnotherOne):
    __super = AnotherOne
    # etc, etc
    def somemethod(self, foo, bar):
        print "before"
        self.__super.somemethod(self, foo, bar)
        print "after"

Metaprogramming magick was deemed inappropriate here (as I
read the group consensus).  One *might* have a global function
called super(), which tried to gauge the appropriate class
object to use (in single-inheritance cases, only, of course),
but it would have to be very black magic indeed (there seems
to be no 'normal' metaprogramming way to discover what class
the calling-method belongs to) as well as liable to break
under all sorts of circumstances (remember that there is no
guarantee that a method is being used within the class in
which it was defined, nor indeed that it was at all defined
as a method rather than originally defined as a free function
and later also assigned as a method), not to speak of multiple
inheritance.

The solve-it-by-convention-if-you-feel-this-need approach was
thus considered preferable in Python (basically by the same
reasoning through which Stroustrup and the standard committee
settled on it in the context of C++).


Alex






More information about the Python-list mailing list