Yes, it is possible and it is what inheritance in Python does. As @deceze suggests, update the files like below:
human_class.py
:
class human():
def __init__ (self, gender="", age=0, height=0, howHigh=""):
#setting attributes
self.gender = ""
self.age = 0
self.height = 0
self.howHigh = ""
def setHeight(self):
self.height = int(input("What is your height in cm? "))
def setGender(self):
self.gender = input("What is your gender? ")
def setAge(self):
self.age = int(input("What is your age? "))
def changeHeight(self):
if self.height < 80:
self.howHigh = "Small"
print("Your hieght is small!")
elif self.height >= 80 and self.height < 180:
self.howHigh = "Medium"
print("Your hieght is medium!")
elif self.height >= 180:
self.howHigh = "Large"
print("Your hieght is tall")
if __name__ == '__main__':
human1 = human()
human1.setHeight()
human1.setGender()
human1.setAge()
print("human gender is ", human1.gender)
print("Human age is", human1.age)
print("Human height is", human1.height)
human1.changeHeight()
print(human1.howHigh)
human1 = human()
human1.setHeight()
human1.setGender()
human1.setAge()
print("human gender is ", human1.gender)
print("Human age is", human1.age)
print("Human height is", human1.height)
human1.changeHeight()
print(human1.howHigh)
chlid_class.py
:
from human_class import *
class child(human):
def __init__(self):
super().__init__()
def setHeight(self):
self.height = int(input("What is your height in cm? "))
def changeHeight(self):
if self.height < 30:
self.howHigh = "Small"
print("Your hieght is small for a child!")
elif self.height >= 30 and self.height < 120:
self.howHigh = "Medium"
print("Your hieght is medium for a child!")
elif self.height >= 120:
self.howHigh = "Large"
print("Your hieght is tall for a child!")
child1 = child()
child1.setHeight()
child1.changeHeight()
print(child1.howHigh)
What does if __name__ == '__main__':
do?
From official documentation
'__main__'
is the name of the scope in which top-level code executes. A module’s__name__
is set equal to'__main__'
when read from standard input, a script, or from an interactive prompt.
if __name__ == "__main__":
# execute only if run as a script
solved Python OOP Sub class prints parent class methods then its own methods [duplicate]