Purpose of polymorphism in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time cost changes when using polymorphism in Python programs.
Specifically, we ask: How does polymorphism affect the number of steps a program takes as input grows?
Analyze the time complexity of this polymorphic code snippet.
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof"
class Cat(Animal):
def speak(self):
return "Meow"
animals = [Dog(), Cat(), Dog()]
for animal in animals:
print(animal.speak())
This code creates different animals and calls their speak method using polymorphism.
Look for loops or repeated calls that affect time.
- Primary operation: Looping over the list of animals and calling speak()
- How many times: Once for each animal in the list
As the number of animals grows, the program calls speak() for each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to speak() |
| 100 | 100 calls to speak() |
| 1000 | 1000 calls to speak() |
Pattern observation: The number of operations grows directly with the number of animals.
Time Complexity: O(n)
This means the time grows in a straight line as the number of animals increases.
[X] Wrong: "Polymorphism makes the program slower because it adds extra steps."
[OK] Correct: Polymorphism itself just lets us call methods on different objects easily. The time depends mostly on how many objects we have, not on polymorphism adding extra loops.
Understanding how polymorphism affects time helps you explain your code clearly and shows you know how design choices impact performance.
"What if we added nested loops calling speak() multiple times per animal? How would the time complexity change?"
Practice
polymorphism in Python programming?Solution
Step 1: Understand the meaning of polymorphism
Polymorphism means one action can behave differently depending on the object it is acting on.Step 2: Match the purpose with the options
To allow one function or method to work in different ways depending on the object correctly describes this behavior, while others describe unrelated concepts.Final Answer:
To allow one function or method to work in different ways depending on the object -> Option AQuick Check:
Polymorphism = One action, many behaviors [OK]
- Confusing polymorphism with speed optimization
- Thinking polymorphism is about storing multiple values
- Mixing polymorphism with data type creation
Solution
Step 1: Recall how polymorphism works with methods
Polymorphism allows methods with the same name to behave differently in different classes.Step 2: Check which option matches this behavior
Define methods with the same name in different classes and call them on their objects correctly describes defining same-named methods in different classes and calling them on their objects.Final Answer:
Define methods with the same name in different classes and call them on their objects -> Option BQuick Check:
Same method name, different classes = polymorphism [OK]
- Thinking polymorphism means different method names
- Ignoring method overriding in subclasses
- Using global variables to control method behavior
class Dog:
def sound(self):
return "Bark"
class Cat:
def sound(self):
return "Meow"
animals = [Dog(), Cat()]
for animal in animals:
print(animal.sound())Solution
Step 1: Understand the classes and their methods
Dog and Cat classes both have a method named sound that returns different strings.Step 2: Trace the loop calling sound on each object
The loop calls sound() on Dog instance (returns "Bark") and Cat instance (returns "Meow"), printing each.Final Answer:
Bark Meow -> Option DQuick Check:
Different classes, same method name, different outputs [OK]
- Assuming both calls return the same string
- Expecting a runtime error due to method name
- Mixing the order of outputs
class Bird:
def fly(self):
print("Flying")
class Penguin(Bird):
def fly(self):
print("Cannot fly")
p = Penguin()
p.fly()Solution
Step 1: Check method overriding in subclass
Penguin overrides fly method to print "Cannot fly", which is valid polymorphism.Step 2: Verify code execution
Creating Penguin object and calling fly prints "Cannot fly" without error.Final Answer:
No error; code correctly uses polymorphism -> Option AQuick Check:
Overriding method in subclass is correct polymorphism [OK]
- Thinking overriding is an error
- Expecting method must return a value
- Believing super() call is mandatory
draw() method, regardless of the object's class. Which concept does this best illustrate?Solution
Step 1: Understand the function requirement
The function calls draw() on any object without knowing its class.Step 2: Identify the concept allowing this behavior
Polymorphism allows different objects to respond to the same method call appropriately.Final Answer:
Polymorphism -> Option CQuick Check:
Same method call, different objects = polymorphism [OK]
- Confusing with inheritance which is about class hierarchy
- Mixing with encapsulation which hides data
- Thinking abstraction means calling any method
