Why does super take a class name as the argument?
Ed Leafe
ed at leafe.com
Fri Oct 15 21:35:47 EDT 2004
On Oct 15, 2004, at 7:36 PM, dataangel wrote:
> What's the going argument against it? Makes sense to me.
There are constructions in other languages whereby an call to the
superclass method does not require an explicit name of the current
class. When an inherited method is augmented in a subclass, the call to
the superclass version of the method doesn't require any class
specification; each class knows its own hierarchy.
So in the examples others have given, you'd have:
class A(object):
def theMethod(self):
print "doing A stuff"
def someOtherMethod(self):
print "Some Other A method is running"
class B(A):
def theMethod(self):
print "doing some early B stuff"
self.super()
print "some final B stuff"
When B is created and its theMethod() is called, it prints as follows:
>>> obj = B()
>>> obj.theMethod()
doing some early B stuff
doing A stuff
some final B stuff
>>> obj.someOtherMethod()
Some Other A method is running
In other words, the B class knows it inherits from the A class. When
the call to super() is encountered, B looks to its superclass for such
a method, and executes it if found. If it has multiple parents, it is
resolved as is normally done with multiple inheritance. When the call
to someOtherMethod() is made, the B object knows that it should execute
that method is its superclass.
Think of it another way: since B can figure out how to execute a call
to a method that doesn't exist in B itself by looking into its class
hierarchy, why can't a call like self.super() execute the code that
would have been executed had the subclass not overridden the method in
the first place?
___/
/
__/
/
____/
Ed Leafe
http://leafe.com/
http://dabodev.com/
More information about the Python-list
mailing list