Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Inheritance constructor with parameters python

class Animal:
    def __init__(self, legs, name):
        self.eyes = 2
        self.legs = legs
        self.name = name

    def breathe(self):
        print("Inhale, exhale")
    
    def __str__(self):
        return f"I'm an animal with {self.legs} legs, I'm also called {self.name}"

class Fish(Animal):
    def __init__(self, legs, name):
        super(Fish, self).__init__(legs, name)
    
    def swim(self):
        print("Swimming")

    def __str__(self):
        return f"I'm a fish with {self.legs} legs, I'm also called {self.name}"
    
fish = Fish(0, "Nemo")
fish.breathe()
fish.swim()

print(fish)
 
PREVIOUS NEXT
Tagged: #Inheritance #constructor #parameters #python
ADD COMMENT
Topic
Name
9+2 =