I tried this inheritance code and got an unexpected output. Kindly guide me where is my fault.
Child.py
from ParentClass import Parent
class ChildImp(Parent):
var2 = 200
def __init__(self):
Parent.__init__(self, 8, 3)
def getData(self):
self.var1 + self.Sub() + self.Add()
obj = ChildImp()
print(obj.getData())
ParentClass.py
class Parent:
var1 = 100
def __init__(self, a, b):
self.firstNUm = a
self.secondNUm = b
def Add(self):
return self.firstNUm + self.secondNUm
def Sub(self):
return self.firstNUm - self.secondNUm
obj1 = Parent(4, 6)
print(obj1.Add())
obj2 = Parent(9, 2)
print(obj2.Sub())
output:
10
7
None
Process finished with exit code 0
Where does this 10 and 7 come from? Why there is a None in the output?
ParentClass.pydoesn't have its code wrapped in anif __name__ == '__main__':statement, that file's code will be executed when it is imported.