Duck Typing
Introduction to OOP's
Class
Object
Constructor
Instance Variables
Class Variables
Instance Methods
self Keyword
Encapsulation
Public Members
Protected Members
Private Members
Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
super() Function
Polymorphism
Method Overriding
Duck Typing
Abstraction
Abstract Class
Abstract Method
Class Method
Static Method
Special Methods
_str_ Method
Operator Overloading
Composition
Aggregation
Association
Method Resolution Order
Object Class
issubclass() and isinstance()
Property Decorator
Setter Method with @property
Destructor
Inner Class
Complete Mini Example
Duck Typing
21. Duck Typing
Duck typing in Python means an object's suitability depends on the methods it provides rather than its exact class type. This fits Python's flexible object model and is often used to explain practical polymorphism.
Syntax
obj.method_name()
Example
class Duck:
def speak(self):
print("Quack")
class Person:
def speak(self):
print("Hello")
def call_speak(obj):
obj.speak()
call_speak(Duck())
call_speak(Person())
Output
Quack
Hello
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Duck Typing to test your knowledge.
Browse Quizzes »