[Edu-sig] Python @ Education: What are your problems?
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Fri, 7 Jun 2002 11:55:45 -0700 (PDT)
> - no super() method call to call to super-class (AFAIK)
In Python versions previous to 2.2, this could be done with the __bases__
attribute:
###
>>> class Parent:
... def sayHello(self):
... print "hello"
...
>>> class Child(Parent):
... def sayHello(self):
... print "My mom says:",
self.__class__.__bases__[0].sayHello(self)
...
>>> p, c = Parent(), Child()
>>> p.sayHello()
hello
>>> c.sayHello()
My mom says: hello
###
but as you can tell, this is really awkward! Multiple inheritance,
combined with the dynamic features of Python (__getattr__/__setattr__),
can complicate things.
Python 2.2 has a new "super()" function that simplifies this a lot.
With super(), the example above looks like:
###
class Parent(object):
def sayHello(self): print "Hello!"
class Child(Parent):
def sayHello(self):
print "My mom says", super(Child, self).sayHello()
###
See:
http://www.python.org/doc/current/whatsnew/sect-rellinks.html
for details on this new function.
Hope this helps!