Search
 
SCRIPT & CODE EXAMPLE
 

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

python constructor inheritance

# this is the class which will become
# the super class of "Subclass" class
class Class():
    def __init__(self, x):
        print(x)
  
# this is the subclass of class "Class"
class SubClass(Class):
    def __init__(self, x):
  
        # this is how we call super
        # class's constructor
        super().__init__(x)
  
# driver code
x = [1, 2, 3, 4, 5]
a = SubClass(x)
Comment

PREVIOUS NEXT
Code Example
Python :: fyit download 
Python :: extract column numpy array python 
Python :: generic python 
Python :: How to install XGBoost package in python using conda 
Python :: pandas map multiple columns 
Python :: python make a new window 
Python :: how to make images in python 
Python :: python rsa 
Python :: python process memory usage 
Python :: numpy generate random 2d array 
Python :: python empty dictionary 
Python :: df empty 
Python :: python ascii caesar cipher 
Python :: install pip with pacman linux 
Python :: how to 404 custom page not found in django 
Python :: python remove new line 
Python :: python replace all values in a column 
Python :: how to convert string date to timestamp in python 
Python :: check pandas version 
Python :: datetime to unix timestamp milliseconds python 
Python :: list to sentence python 
Python :: how to underline text in tkinter 
Python :: python append to csv on new line 
Python :: pandas sort 
Python :: what is pypy 
Python :: python input 
Python :: erase % sign in row pandas 
Python :: cut part of video ffmpeg 
Python :: python currency signs 
Python :: python date from string 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =