H
hermy
Hi,
I'm trying to figure out how to pass constructor arguments to my
superclasses in a multiple inheritance situation.
As I understand it, using super() is the preferred way to call
the next method in method-resolution-order. When I have parameterless
__init__ methods, this works as expected.
However, how do you solve the following simple multiple inheritance
situation in python ?
class A(object):
def __init__(self,x):
super(A,self).__init__(x)
print "A init (x=%s)" % x
class B(object):
def __init__(self,y):
super(B,self).__init__(y)
print "B init (y=%s)" % y
class C(A,B):
def __init__(self,x,y):
super(C,self).__init__(x,y) <-------- how to do this ???
print "C init (x=%s,y=%s)" % (x,y)
What I want is that when I create a class C object
x = C(10,20)
that the x argument of C's __init__ is used to initialize the
A superclass, and the y argument is used to initialize the B
superclass.
In C++, I would do this using initilaization lists, like:
C::C(int x, int y) : A(x), B(y) { ... }
I'm probably overlooking some basic stuff here, but I haven't
been able to figure this out. Googling got me lots of examples,
but all with empty __init__ argument lists (which obviously works,
but is too trivial in practice).
regards,
herman
I'm trying to figure out how to pass constructor arguments to my
superclasses in a multiple inheritance situation.
As I understand it, using super() is the preferred way to call
the next method in method-resolution-order. When I have parameterless
__init__ methods, this works as expected.
However, how do you solve the following simple multiple inheritance
situation in python ?
class A(object):
def __init__(self,x):
super(A,self).__init__(x)
print "A init (x=%s)" % x
class B(object):
def __init__(self,y):
super(B,self).__init__(y)
print "B init (y=%s)" % y
class C(A,B):
def __init__(self,x,y):
super(C,self).__init__(x,y) <-------- how to do this ???
print "C init (x=%s,y=%s)" % (x,y)
What I want is that when I create a class C object
x = C(10,20)
that the x argument of C's __init__ is used to initialize the
A superclass, and the y argument is used to initialize the B
superclass.
In C++, I would do this using initilaization lists, like:
C::C(int x, int y) : A(x), B(y) { ... }
I'm probably overlooking some basic stuff here, but I haven't
been able to figure this out. Googling got me lots of examples,
but all with empty __init__ argument lists (which obviously works,
but is too trivial in practice).
regards,
herman