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 ...
Get Python Made Easy now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.