0
0
Pythonprogramming~10 mins

Polymorphism through functions in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Polymorphism through functions
Define function with parameter obj
Call method on obj
Python finds method in obj's class
Execute method specific to obj's class
Return result
Function output varies by obj type
The function calls a method on any object passed in. Python runs the version of the method that belongs to the object's class, allowing different behaviors.
Execution Sample
Python
class Dog:
    def speak(self):
        return "Woof!"

def animal_sound(animal):
    return animal.speak()

print(animal_sound(Dog()))
This code defines a Dog class with a speak method and a function that calls speak on any animal object.
Execution Table
StepActionEvaluationResult
1Define class Dog with method speakNo outputDog class ready
2Define function animal_sound with parameter animalNo outputFunction ready
3Call animal_sound with Dog instanceanimal = Dog()Dog object created
4Inside animal_sound, call animal.speak()Calls Dog.speak()"Woof!" returned
5animal_sound returns "Woof!"Return value "Woof!""Woof!" printed
6Program endsNo more codeExecution stops
💡 Program ends after printing the dog's sound
Variable Tracker
VariableStartAfter Step 3After Step 4Final
animalundefinedDog instanceDog instanceDog instance
return valueundefinedundefined"Woof!""Woof!"
Key Moments - 2 Insights
Why does animal.speak() call Dog's speak method and not some other method?
Because animal is a Dog instance at step 4, Python looks up speak in Dog's class and runs that method, as shown in execution_table row 4.
What if we pass a different animal with a different speak method?
The function calls that object's speak method instead, showing polymorphism. The function doesn't care about the type, only that speak exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what method is called?
Aprint()
BDog.speak()
Canimal_sound()
DNone
💡 Hint
Check the 'Evaluation' column at step 4 in execution_table
At which step does the function return the string "Woof!"?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Result' column for step 5 in execution_table
If we pass an object without a speak method, what will happen during execution?
APython raises an AttributeError at step 4
BThe function returns None
CThe function prints nothing
DThe program ends normally
💡 Hint
Recall that at step 4, animal.speak() is called; if speak doesn't exist, Python errors
Concept Snapshot
Polymorphism through functions:
- Define a function that calls a method on its parameter.
- Pass different objects with that method.
- Python runs the method of the object's class.
- Function behavior changes based on object type.
- No need to check object type explicitly.
Full Transcript
This example shows polymorphism through functions in Python. We define a Dog class with a speak method. Then we define a function animal_sound that calls speak on any object passed in. When we call animal_sound with a Dog instance, Python runs Dog's speak method and returns "Woof!". The execution table traces each step: defining classes and functions, creating objects, calling methods, and returning results. The variable tracker shows how the variable animal holds the Dog instance and how the return value changes. Key moments clarify why the correct method is called and what happens if a different object is passed. The quiz tests understanding of method calls, return steps, and error cases. This teaches how functions can work with different objects sharing the same method name, a core idea of polymorphism.