I have this code:
class A(object):
def __init__(self):
print " A"
class B(A):
def __init__(self):
print "B"
x=B()
print "Done"
the result is: "B" gets printed why does it not print "A", eventhough class B inheritance A
I have this code:
class A(object):
def __init__(self):
print " A"
class B(A):
def __init__(self):
print "B"
x=B()
print "Done"
the result is: "B" gets printed why does it not print "A", eventhough class B inheritance A
If you want to use A's __init__ while also using B's __init__, then try:
class A(object):
def __init__(self):
print " A"
class B(A):
def __init__(self):
A.__init__(self)
print "B"
x=B()
print "Done"
Or, if you would prefer not to mention the superclass by name:
class A(object):
def __init__(self):
print " A"
class B(A):
def __init__(self):
super(B, self).__init__()
print "B"
x=B()
print "Done"
Both of these produce the output:
A
B
Done
super(self.__class__, self). It fails as soon as you make a descendant of B and self.__class__ isn't B any more.