What is Polymorphism in OOPs in python

This recipe explains what is Polymorphism in OOPs in python

Recipe Objective

This recipe explains what is polymorphism.

Deep Learning Project for Text Detection in Images using Python

Polymorphism

Polymorphism is the ability to use a common interface for multiple data types. In the below program we have created two classes Crocodile and Sloth. Both the classes have a common climb() method but their functions are different. To implement polymorphism we created a common interface i.e climb_test() function that takes an object and calls the object's climb() method. In the end, we have passed croc and slow objects in climb_test() function.

 

    class Crocodile:

        def swim(self):
        print("Crocodile can swim")
    
        def climb(self):
        print("Crocodile can't climb tree")

    class Sloth:

        def swim(self):
        print("Sloth can't swim")
    
        def climb(self):
        print("Sloth can climb tree")

    # common interface
    def climp_test(Sloth):
        Sloth.climb()

    #instantiate objects
    croc = Crocodile()
    slow = Sloth()

    # passing the object
    climp_test(croc)
    climp_test(slow)    
    

 

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.