Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python code to demonstrate inheritance with animal class

class Animal():
    def __init__(self, name, age):
        self.name = name
        self.age = age 

    def speak(self):
        print("I am", self.name, "and I am", self.age, "years old")


class Dog(Animal):
    def __init__(self, name, age)
        self.name = name
        self.age = age
        self.type = "dog"

    # Since we inherit from the animal class we can use the method speak on Dog objects


tim = Dog("Tim", 5)
tim.speak() # This will print "I am Tim and I am 5 years old"
Comment

PREVIOUS NEXT
Code Example
Python :: form field required in django views 
Python :: split column and rename them 
Python :: email confirmation django 
Python :: python manual elif 
Python :: how to print list without newline 
Python :: python unicode function 
Python :: null=true django 
Python :: splitting on basis of regex python 
Python :: check file existtnece python 
Python :: python split space or tab 
Python :: how to convert data into numerical dataset 
Python :: python Python Program to Catch Multiple Exceptions in One Line 
Python :: activate python venv in windows 
Python :: get value of bit in integer python 
Python :: tar dataset 
Python :: Python how to use __floordiv__ 
Python :: binary search iterative 
Python :: telegram bot documentation python 
Python :: List get both index and value. 
Python :: python meanGroups(a): 
Python :: regex in python 
Python :: python extraer ultimo elemento lista 
Python :: print f python 
Python :: copy along additional dimension numpy 
Python :: add button to python gui file 
Python :: string to list 
Python :: seaborn heatmap center xticks 
Python :: how to search for an item in a list in python 
Python :: Adding two lists using map() and Lamda Function 
Python :: channel unhiding command in discord.py 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =