M
Michael J. Fromberger
Consider the following class hierarchy in Python:
class A (object):
def __init__(self):
print "cons A"
class B (object):
def __init__(self):
print "cons B"
class C (A):
def __init__(self):
super(C, self).__init__()
print "cons C"
class D (B):
def __init__(self):
super(D, self).__init__()
print "cons D"
Now, suppose I would like to define a new class as follows:
class E (C, D):
...
In the constructor for E, I would like to invoke the constructors of
both parent classes. The correct way to do this seems to be exemplified
by the following:
class E (C, D):
def __init__(self):
super(E, self).__init__() # calls C constructor
super(A, self).__init__() # calls D constructor (**)
print "cons E"
This works, but I find it somewhat troubling. It seems to me that one
should not have to "know" (i.e., write down the names of) the ancestors
of C in order to dispatch to superclass methods in D, since C and D
share no common ancestors south of object.
Is there a better (i.e., more elegant) way to handle the case marked
(**) above?
Curious,
-M
class A (object):
def __init__(self):
print "cons A"
class B (object):
def __init__(self):
print "cons B"
class C (A):
def __init__(self):
super(C, self).__init__()
print "cons C"
class D (B):
def __init__(self):
super(D, self).__init__()
print "cons D"
Now, suppose I would like to define a new class as follows:
class E (C, D):
...
In the constructor for E, I would like to invoke the constructors of
both parent classes. The correct way to do this seems to be exemplified
by the following:
class E (C, D):
def __init__(self):
super(E, self).__init__() # calls C constructor
super(A, self).__init__() # calls D constructor (**)
print "cons E"
This works, but I find it somewhat troubling. It seems to me that one
should not have to "know" (i.e., write down the names of) the ancestors
of C in order to dispatch to superclass methods in D, since C and D
share no common ancestors south of object.
Is there a better (i.e., more elegant) way to handle the case marked
(**) above?
Curious,
-M