Your First Steps in Python: A Beginner to Intermediate Guide

Polymorphism: Objects Behaving in Different Ways

Section 6

Object-Oriented Programming (OOP) Basics

Your First Steps in Python: A Beginner to Intermediate GuideObject-Oriented Programming (OOP) Basics

Welcome back to our Python OOP journey! We've explored classes, objects, inheritance, and encapsulation. Now, let's dive into a concept that makes OOP truly powerful and flexible: Polymorphism. The word 'polymorphism' comes from Greek words meaning 'many forms'. In programming, it refers to the ability of different objects to respond to the same method call in their own specific ways.

Imagine you have a 'draw' command. If you tell a 'Circle' object to draw, it will draw a circle. If you tell a 'Square' object to draw, it will draw a square. They both understand the 'draw' command, but they execute it differently based on their inherent nature. This is polymorphism in action!

In Python, polymorphism is often achieved through method overriding (which we touched upon in inheritance) and duck typing. Duck typing is a philosophy where 'if it walks like a duck and quacks like a duck, then it must be a duck.' In Python, we don't need explicit interfaces or abstract classes to achieve polymorphism. If an object has a method with a specific name, you can call that method, and Python will figure out how to execute it for that particular object.

Let's see this with a practical example. We'll create a base class 'Animal' with a 'speak' method, and then create subclasses 'Dog' and 'Cat' that override this method.

class Animal:
    def speak(self):
        pass  # This method will be overridden by subclasses

class Dog(Animal):
    def speak(self):
        return 'Woof!'

class Cat(Animal):
    def speak(self):
        return 'Meow!'

Now, let's create instances of these classes and call the 'speak' method on them. Notice how the output differs, even though we're calling the same method name.

dog = Dog()
cat = Cat()

print(dog.speak())
print(cat.speak())

The output will be: Woof! Meow!

Here, both dog and cat objects respond to the speak() method, but each provides its own unique implementation. This is a clear demonstration of polymorphism.

チャプターへ戻る