0

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?

2
  • 2
    Becauase ParentClass.py doesn't have its code wrapped in an if __name__ == '__main__': statement, that file's code will be executed when it is imported. Commented Dec 1, 2021 at 17:27
  • getData() returns None. You have print(obj.getData()) so it prints None. Commented Dec 1, 2021 at 17:27

3 Answers 3

1

The output is from each of the following calls

10     # print(obj1.Add())
7      # print(obj2.Sub())
None   # print(obj.getData())

note that Add and Sub will return the computed value, btu getData has no return so will implicitly return None hence the last output.

Note that the reason those Add and Sub lines are executing is because of the import statement. If you only want those to run when that script is directly invoked you would modify that block to

if __name__ == '__main__':
    obj1 = Parent(4, 6)
    print(obj1.Add())
    obj2 = Parent(9, 2)
    print(obj2.Sub())
Sign up to request clarification or add additional context in comments.

1 Comment

In addition if I were you I would explain why print(obj1.Add()) and print(obj2.Sub()) are being executed in the first place.
0

Output is from the 3 print statements.

print(obj1.Add())    # prints 10
print(obj2.Sub())    # prints 7
print(obj.getData()) # prints None

Need a return on ChildImp getData() function otherwise returns None.

from ParentClass import Parent

class ChildImp(Parent):
    var2 = 200

    def __init__(self):
        Parent.__init__(self, 8, 3)

    def getData(self):
        return self.var1 + self.Sub() + self.Add() # <== added return

obj = ChildImp()
print(obj.getData())

Also good practice to add if name == __main__ check in the parent class so only the main in the class called is executed.

# ParentClass.py
class Parent:
    ...

if __name__ == '__main__':

    obj1 = Parent(4, 6)
    print(obj1.Add())
    obj2 = Parent(9, 2)
    print(obj2.Sub())

1 Comment

Keep in mind self.var1 should actually be Parent.var1
0

10 and 7 are the return values of the Add and Sub methods

The None is there because getData does not return anything

For all three values there is a print call.

1 Comment

As there is a call to print in each file that is not inside a function, it is executed when you import or run the python file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.