DevZone Logo

Dynamic Duck Typing in Python - If It Quacks Like a Duck

FS

MD Fahid Sarker

Senior Software EngineerJuly 7, 2024


-
-
Share
Bookmark

Dynamic Duck Typing in Python: If It Quacks Like a Duck

Have you ever wondered if your code could be as flexible as a rubber duck? 馃 Well, wonder no more because today we're diving into the world of Duck Typing in Python. It's a core concept that can make your code not only flexible but also delightfully fun. Let's take a quack at it!

What is Duck Typing?

In Python, Duck Typing is the way the language handles types based solely on the methods and properties rather than explicit class inheritance. The term 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."

In simpler terms, with Duck Typing, if an object behaves like a particular type (has the requisite methods and properties), it is that type.

Why Duck Typing?

The main goal is to promote flexibility and ease of use within our code. Rather than adhering to rigid type constraints, we focus on what objects can do.

An Example to Quack About

Let鈥檚 see Duck Typing in action with an example. Suppose you want to create a function that accepts any object as long as it has a quack method and a fly method.

Code.python
class Duck: def quack(self): print("Quack!") def fly(self): print("Flying high!") class Person: def quack(self): print("I'm pretending to be a duck!") def fly(self): print("I wish I could fly...") def perform_actions(entity): entity.quack() entity.fly() daffy = Duck() john = Person() perform_actions(daffy) perform_actions(john)

Output

Output
Quack! Flying high! I'm pretending to be a duck! I wish I could fly...

Despite Person not being a Duck class, it can still participate in perform_actions because it implements quack and fly methods.

Solving Problems with Duck Typing

Rigid typing or enforcing strict class-based architectures often leads to code that is difficult to maintain and extend. This is especially true in dynamically changing environments or in projects with heavy reliance on third-party modules. Duck Typing can alleviate these constraints by allowing different objects to work interchangeably if they share the same interface contract.

The Issue: Rigid Class Constraints

Consider the following problem: You've committed to an Animal superclass with subclasses Duck and Sparrow. Suddenly, a new requirement demands that Person class instances also use the same method designed for Animal.

Code.python
class Animal: def sound(self): raise NotImplementedError("Subclass must implement abstract method") class Duck(Animal): def sound(self): print("Quack") class Sparrow(Animal): def sound(self): print("Chirp") class Person: def sound(self): print("Hello!") def make_sound(obj): if isinstance(obj, Animal): obj.sound() else: print("Not an Animal!") daffy = Duck() jack = Person() make_sound(daffy) # Works make_sound(jack) # Error: Not an Animal!

Solution: Enter Duck Typing

Using Duck Typing, we can streamline make_sound to work with any object that follows the expected 'interface'.

Code.python
def make_sound(entity): try: entity.sound() except AttributeError as e: print("This entity doesn't sound!") make_sound(daffy) # Works make_sound(jack) # Works too!

No more errors! Isn't that music to your ears?

Conclusion

Duck Typing is a fantastic feature that allows Python developers to write more flexible, reusable, and maintainable code. By focusing on what objects can do rather than what they are, you promote a architecture that can easily adapt to new requirements. 馃

So the next time you're writing Python code, remember: if it quacks like a duck, it probably is one!

Quack

Happy coding! 馃帀

Found the blog helpful? Consider sharing it with your friends.
Buy Me a Coffee

PythonDuck TypingDynamic TypingOOP

Related Blogs

Blogs that you might find interesting based on this blog.