0

Two Classes. I have a new object in the parent class that gives attributes a value, and a calling method that displays them. Then I create a child class in a separate file and inherit from the parent class.

 class Vehicle:

    def __init__(self, brand, model):   

        self.brand = brand 
        self.model = model 

    def show_description(self):
        print('Brand: ', self.brand, 'Model: ', self.model)

volvo = Vehicle('Volvo','L350H') 
volvo.show_description()  



#new class in separate file:
# ps. import not correct?

from vehicle import vehicle

class Excavator(Vehicle):
    
    def __init__(self, brand, model):
        super.__init__(brand, model)


hitachi = Excavator('Hitachi','EX8000')
hitachi.show_description()


Output: Brnad: Volvo Model: L350H
        Brand: Hitachi Model: EX8000

  

Then I create object for Excavator class and I call show_description (). When I run the Excavator class file, it will also show me the print of the Vehicle, (Volvo). And my goal was to use the show_descriptipn () method - from Vehicle, only to display the new object (Hitachi) in the Excavator class. So, by calling a method from the parent class, we will get the result of both classes? We inherit everything, even the created objects? Or maybe I am doing something wrong in this case, or I don`t understand something. Could someone explain this?

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
simply_red
  • 29
  • 9
  • "So, by calling a method from the parent class, we will get the result of both classes?" No. Because you `import` the module, which *executes the module*. In the module, you do `volvo.show_description()`, so of course, it outputs `Brnad: Volvo Model: L350H` – juanpa.arrivillaga Jan 26 '21 at 16:19
  • 1
    When you import a file, the entire file is executed - including the creation of `volvo` and printing its description, in your case. – jasonharper Jan 26 '21 at 16:19
  • 1
    @simply_red: Might be relevant: https://stackoverflow.com/questions/419163/what-does-if-name-main-do – Maurice Meyer Jan 26 '21 at 16:21
  • As an aside, use *lowercase* names for modules, not uppercase. Also note, you didn't create the objects *in the class*. – juanpa.arrivillaga Jan 26 '21 at 16:22
  • @simply_red yes, of course you have to import it, how else would the class exist? Go ahead and try, it will throw a `NameError`. The location of the files in your file system is completely irrelevant. – juanpa.arrivillaga Jan 26 '21 at 16:29
  • @juanpa.arrivillaga ah yes, unless we have both classes in one file. Here import was making me confused ... import / inheritance, where import imports and displays methods, doesn't that conflict with inheritance in my case? . Turns out, I have to use if__name__ main – simply_red Jan 26 '21 at 16:53
  • 1
    @simply_red this has *nothing* to do with inheritance. Nothing at all. Again, it's because **you wrote** `volvo.show_description()`. So *of course* when you run that module, it will `volvo.show_description()`. – juanpa.arrivillaga Jan 26 '21 at 17:04

1 Answers1

1

Use ole if __name__ == "__main__":

Vehicle.py

class Vehicle:

    def __init__(self, brand, model):   

        self.brand = brand 
        self.model = model 

    def show_description(self):
        print('Brand: ', self.brand, 'Model: ', self.model)
if __name__ == "__main__":
  volvo = Vehicle('Volvo','L350H') 
  volvo.show_description()

Excavator.py

from Vehicle import Vehicle

class Excavator(Vehicle):
    
    def __init__(self, brand, model):
        super.__init__(brand, model)

if __name__ == "__main__":
  hitachi = Excavator('Hitachi','EX8000')
  hitachi.show_description()

This way, if you run either module it will only output what is defined in the if __name__ == "__main__": section.

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46