
Shobha
The best way to predict the future is to invent it
What is duck typing in Python?
Duck typing in Python means that the type or class of an object is determined by its behavior (methods and attributes), not by its actual type. If an object behaves like a certain type (e.g., has a specific method), Python will treat it as that type, regardless of its actual class.
Example:
class Dog:
def speak(self):
return "Woof!"
def animal_sound(animal):
return animal.speak()
print(animal_sound(Dog())) # Output: Woof!
Example:
class Dog:
def speak(self):
return "Woof!"
def animal_sound(animal):
return animal.speak()
print(animal_sound(Dog())) # Output: Woof!