Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

demonstrating polymorphism in python class

class India():
    def capital(self):
        print("New Delhi is the capital of India.")
 
    def language(self):
        print("Hindi is the most widely spoken language of India.")
 
    def type(self):
        print("India is a developing country.")
 
class USA():
    def capital(self):
        print("Washington, D.C. is the capital of USA.")
 
    def language(self):
        print("English is the primary language of USA.")
 
    def type(self):
        print("USA is a developed country.")
 
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
    country.capital()
    country.language()
    country.type()
Comment

Using Polymorphism in Python

class Parrot:
    def fly(self):
        print("Parrot can fly")
        def swim(self):
        print("Parrot can't swim")
class Penguin:
    def fly(self):
        print("Penguin can't fly")
        def swim(self):
        print("Penguin can swim")
# common interface
def flying_test(bird):
    bird.fly()
#instantiate objects
blu = Parrot()
peggy = Penguin()
# passing the object
flying_test(blu)
flying_test(peggy)
Comment

polymorphism in python

# Python program to demonstrate in-built poly-
# morphic functions

# len() being used for a string
print(len("geeks"))

# len() being used for a list
print(len([10, 20, 30]))
Comment

python polymorphism

import math
class Shape:
    def __init__(self, name):
        self.name = name
    def area(self):
        pass
    def __str__(self):
        return self.name

class Square(Shape):
    def __init__(self, length):
        super().__init__("Square")
        self.length = length
    def area(self):
        return self.length**2

class Circle(Shape):
    def __init__(self, radius):
        super().__init__("Circle")
        self.radius = radius
    def area(self):
        return math.pi * self.radius**2 

a = Square(4)
b = Circle(7)
for shape in (a, b):
    print(f'Area of {shape} is {shape.area():.2f}')
# Area of Square is 16.00
# Area of Circle is 153.94
Comment

PREVIOUS NEXT
Code Example
Python :: sorted key python 
Python :: remove a first array of item in python 
Python :: how to make a loop in python 
Python :: Count the number of cells that contain a specific value in a pandas dataframe python 
Python :: print to screen 
Python :: doctest example in python 
Python :: python gitignore 
Python :: python check date between two dates 
Python :: append to list in dict python 
Python :: flip dictionary python 
Python :: pytorch get non diag element 
Python :: python telegram 
Python :: calculate area under the curve in python 
Python :: how to remove new line in python 
Python :: how to open a file in python 
Python :: python scipy put more weight to a set value in curve_fit 
Python :: full row visible in jupyter notebook 
Python :: Convert Int to String Using str() function 
Python :: Python Pandas export Dataframe to csv File 
Python :: import csv as dic 
Python :: python using strip trim white sapce 
Python :: python string ignore characters 
Python :: null=true django 
Python :: how to change the main diagonal in pandas 
Python :: how to make input box if else statement in tkinter 
Python :: stack widgets in tkinter 
Python :: Access python http.server on google colab 
Python :: alphabeticallly 
Python :: spacy create tokenizer 
Python :: string replace in python 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =