multiple inharitance super() question

Peter Otten __peter__ at web.de
Mon Nov 14 13:48:33 CET 2005


Alex Greif wrote:

> BUT what happens if B extends from A and AA like:
> 
> class A(object):
> def __init__(self, args):
> ...
> 
> class AA(object):
> def __init__(self, args):
> ...
> 
> class B(A,AA):
> def __init__(self, args):
> super(B, self).__init__(args)
> 
> 
> How can I tell that B.__init__() should initialize A and AA?
> Or is the new super() so clever to call A.__init__() and AA.__init__()
> internally?

Yes but only if you put a super() call into A/AA.__init__().

class A(object):
    def __init__(self, args):
        print "init A"
        super(A, self).__init__(args)

class AA(object):
    def __init__(self, args):
        print "init AA"
        super(AA, self).__init__(args)


class B(A, AA):
    def __init__(self, args):
        print "init B"
        super(B, self).__init__(args)

if __name__ == "__main__":
    B(42)
 
The most significant constraint of that layout is that all __init__()
methods need compatible signatures.
Of course explicit invocation of baseclass initializers will continue to
work...

Peter




More information about the Python-list mailing list