Duck Typing
suggest changeDuck typing is polymorphism without inheritance. It is available in Python because of its dynamic typing system. As long as the classes contain the same methods, the Python interpreter does not distinguish between them, because the calls are checked only at run time.
class Duck:
def quack(self):
print("Quaaaaaack!")
def feathers(self):
print("The duck has white and gray feathers.")
class Person:
def quack(self):
print("The person imitates a duck.")
def feathers(self):
print("The person takes a feather from the ground and shows it.")
def name(self):
print("John Smith")
def in_the_forest(obj):
obj.quack()
obj.feathers()
donald = Duck()
john = Person()
in_the_forest(donald)
in_the_forest(john)
The output is:
Quaaaaaack!
The duck has white and gray feathers.
The person imitates a duck.
The person takes a feather from the ground and shows it.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents