multiple inharitance super() question

A

Alex Greif

Hi,

Before 2.2 I could initialize multiple super classes like this:
class B(A,AA):
def __init__(self, args):
A.__init__(self,args)
AA.__init__(self,args)

Tutorials say that since python 2.2 superclasses should be initialized
with the super() construct.

class A(object):
def __init__(self, args):
...

class B(A):
def __init__(self, args):
super(B, self).__init__(args)


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?

thanks,
Alex Greif


________________________________________
http://www.fotobuch-xxl.de/
 
P

Peter Otten

Alex said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top