super question

Lee Harr missive at frontiernet.net
Tue Apr 8 11:25:42 EDT 2003


> If all your classes only have one parent, it doesn't make a
> difference.  If your classes a) use multiple inheritance, b) several
> of them define a method with the same name, and c) all of them call
> super() to execute that method, then python does some really neat
> stuff to ensure that all the methods of that name get called, and that
> it occurs in the "correct" order.
> 
> So super() is preferable if a) you're redefining a method but want to
> call the base class method(s), and b) your class is part of a multiple
> inheritance graph OR may be used in a multiple inheritance graph.
> 
> Disclaimer:  I'm new to this stuff myself, so if somebody's got a
> better explanation, please speak up!


I guess I do not get it.


>>> class A(object):
...   def f(self):
...     print 'Af'
... 
>>> class B(object):
...   def f(self):
...     print 'Bf'
... 
>>> class C(A, B):
...   def f(self):
...     super(C, self).f()
... 
>>> c=C()
>>> c.f()
Af


Should this be showing
Af
Bf
?

How would I even get that output?  I know I can do:

class C(A, B):
  def f(self):
    A.f(self)
    B.f(self)


but I can't seem to get that out of super()





More information about the Python-list mailing list