Dynamic and Duck Typing
Duck typing is a concept in programming that focuses on the behavior of objects rather than their specific types. The term “duck typing” comes from the saying, “If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck”. This principle promotes flexibility and code reuse, as it allows objects of different types to be used interchangeably as long as they exhibit the expected behavior. Here’s an example of a duck typed function..
class Duck:
def quack(self):
print(“Quack! I am a duck.”)
class Goose:
def quack(self):
print(“Quack! I am a goose.”)
Here we define two classes, Duck and Goose, representing different types of birds. Both classes have a quack() method ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access