-4

I have two python files in the first one I have the parent class. I created a new file with the sub class. When I run the sub class file the methods in the parent class file run first the the sub class.

the parent class looks like this

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")

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)

the sub class file looks like this

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)

The codes are below and when I run the parent class file human runs. When I run the second file, the child class, then methods for class human() run first then the methods for class child(human). What I want is to run the sub class file and have only those methods run. Is that possible, it happens most of the time i do this this way?

Thanks for your help

A.Wolf
  • 19
  • 1
  • 7
  • 3
    *All* the code in the "parent file" will be executed when you `import` it. If you don't want the code below the class definition to be executed, don't put it in that file. (Or make it conditional with a `if __name__ == '__main__': ...`.) – deceze Oct 30 '17 at 10:25

1 Answers1

0

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
deceze
  • 510,633
  • 85
  • 743
  • 889
arshovon
  • 13,270
  • 9
  • 51
  • 69