Does 'super' exist?

Peter Hansen peter at engcorp.com
Sun Dec 9 00:46:38 EST 2001


Bruce Eckel wrote:
> 
> It's a little hard to tell, does 'super' exist in 2.2? If so, where
> might I find the syntax? Thanks.

There are potentially multiple 'supers', so you have to
be explicit.

>>> class A: pass
>>> class B: pass
>>> class C(A, B): pass
>>> C.__bases__
(<class __main__.A at 007A07FC>, <class __main__.B at 007A005C>)

You can do things like this:

>>> class A:
...   def __init__(self):
...     print 'init A'
...
>>> class B:
...   def __init__(self):
...     print 'init B'
...
>>> class C(A, B):
...   def __init__(self):
...     for base in self.__class__.__bases__:
...        base.__init__(self)
...
>>> c = C()
init A
init B

... or if you want the order of initialization to
be different than the order in which the superclasses
were listed:

>>> class C(A, B):
...   def __init__(self):
...     B.__init__(self)
...     A.__init__(self)
...
>>> c = C()
init B
init A

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list