← Back to Python Course | Chapter 7: OOP | Lesson 6 of 11

Python Polymorphism

Polymorphism का मतलब है अलग-अलग चीज़ें एक ही command पर अपने-अपने तरीके से respond कर सकती हैं, जैसे एक duck और एक dog दोनों speak सुनकर अलग-अलग आवाज़ निकालते हैं। आप उन्हें एक जैसा treat कर सकते हैं जबकि वे अलग-अलग व्यवहार करते हैं।
Syntax
python
class ParentClass:
    def method_name(self):
        # base version

class ChildClass(ParentClass):
    def method_name(self):
        # overridden version

Polymorphism क्या है?

Polymorphism का मतलब है कि अलग-अलग classes के objects बिल्कुल एक ही method call पर अपने-अपने तरीके से respond कर सकते हैं — Dog और Cat पर .speak() call करने से हर एक में अलग code चलता है, पर calling code को यह जानने या परवाह करने की ज़रूरत नहीं कि वह किस specific class से डील कर रहा है।

उदाहरण: What is Polymorphism?

python
class Dog:
    def speak(self):
        print("Woof")

class Cat:
    def speak(self):
        print("Meow")

for animal in [Dog(), Cat()]:
    animal.speak()  # same call, different behavior depending on the actual object

Inheritance के साथ Polymorphism

Polymorphism का सबसे common form inheritance के साथ स्वाभाविक रूप से जुड़ता है: एक base class एक method define करती है, और हर subclass उसे अपने version से override करती है, इसलिए Dog और Cat objects की mixed list पर animal.speak() call करने वाला loop बिना किसी if/else type checking के हर एक के लिए सही आवाज़ पाता है।

उदाहरण: Polymorphism with Inheritance

python
class Animal:
    def speak(self):
        pass  # placeholder, meant to be overridden

class Dog(Animal):
    def speak(self):
        print("Woof")

class Cat(Animal):
    def speak(self):
        print("Meow")

for animal in [Dog(), Cat()]:
    animal.speak()  # each subclass's own override runs

Duck Typing

Python यह नहीं चाहता कि किसी object का किसी specific class से belong होना या किसी specific base से inherit करना ज़रूरी हो ताकि उसे कहीं इस्तेमाल किया जा सके — अगर उस पर call किए जा रहे methods उसके पास हैं, तो इतना काफ़ी है।

यह 'duck typing' का मतलब है कि आप किसी भी object को, जिसके पास .read() method है, file expect करने वाले code को पास कर सकते हैं, चाहे वह literally file हो या नहीं।

उदाहरण: Duck Typing

python
class Duck:
    def read(self):
        print("Duck pretending to read")

def process(obj):
    obj.read()  # works on any object that has a read() method

process(Duck())

Built-in Polymorphic Functions

len() जैसे functions और + जैसे operators इस अर्थ में polymorphic हैं कि वे जिस type को दिए जाते हैं उसके हिसाब से अलग व्यवहार करते हैं — len() किसी string के लिए characters और list के लिए items गिनता है, और + strings को जोड़ता है पर numbers को add करता है, यह सब एक ही syntax के through।

उदाहरण: Built-in Polymorphic Functions

python
print(len("hello"))  # len() counts characters for a string
print(len([1, 2, 3]))  # len() counts items for a list
print("a" + "b")  # + concatenates strings
print(1 + 2)  # + adds numbers

Abstract Base Classes

Abstract base class abc module का इस्तेमाल करके ऐसे method signatures define करती है जिन्हें subclasses को implement करना ज़रूरी है — यह आपको एक compile-time जैसी safety देता है कि हर subclass genuinely वे methods provide करती है जिनकी आपके polymorphic code को उम्मीद है, न कि runtime पर AttributeError के साथ fail होना।

उदाहरण: Abstract Base Classes

python
from abc import ABC, abstractmethod

class Shape(ABC):  # abstract base class, can't be instantiated directly
    @abstractmethod
    def area(self):  # subclasses are required to implement this
        pass

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

print(Square(4).area())
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.