DEV Community

Cover image for If it quacks like a duck ๐Ÿฆ†๐Ÿฅ
JohnKagunda
JohnKagunda

Posted on

If it quacks like a duck ๐Ÿฆ†๐Ÿฅ

Duck typing is an interesting concept to learn and also very valuable when you understand it.

So what is duck typing?

A famous concept was made to make understanding this simpler:

if it quacks like a duck, then it is a duck
Enter fullscreen mode Exit fullscreen mode

Well, let's look at this using a simple example:

class Duck:
    def quack(self):
        print("Quack!")

class Goose:
    def quack(self):
        print("Quack!")

class Dog:
    def bark(self):
        print("Woof!")
Enter fullscreen mode Exit fullscreen mode

In the example above, a duck quacks as well as a goose, while a dog only barks. According to duck typing, we can agree that a duck and a goose may as well both be ducks.

So the next question is:

Why is this important?

Let's use already existing components min and max inbuilt functions in Python.
Duck typing is one of the reasons why they are so efficient in getting the minimum and the maximum values.

Comparability

min checks for the method __lt__ inside the object called, regardless of the class, while max checks for the method __gt__.

Iterability

The collection passed to min or max must be iterable. This means it should implement the __iter__ method, which allows the function to traverse through the elements of the collection.

Here is a simple example:

class ComparableObject:
    def __init__(self, value):
        self.value = value

    def __lt__(self, other):
        return self.value < other.value

    def __gt__(self, other):
        return self.value > other.value

    def __repr__(self):
        return f'ComparableObject({self.value})'

a = ComparableObject(10)
b = ComparableObject(20)
c = ComparableObject(5)

objects = [a, b, c]

print(min(objects))  # Outputs: ComparableObject(5)
print(max(objects))  # Outputs: ComparableObject(20)
Enter fullscreen mode Exit fullscreen mode

In this example, ComparableObject implements the __lt__ and __gt__ methods, allowing instances of this class to be compared using the < and > operators. This makes them suitable for use with min and max.

Duck Typing in Action:

  • When min(objects) is called, Python checks if each element in objects can be compared using the < operator. It doesn't care about the type of the elements as long as they can be compared.
  • Similarly, when max(objects) is called, Python checks if each element can be compared using the > operator.

Because ComparableObject implements the necessary comparison methods, min and max work with instances of this class, demonstrating duck typing. The focus is on the presence of the required methods (__lt__ and __gt__), not on the specific type of the objects.

In conclusion, duck typing is a powerful and flexible programming paradigm that emphasizes an object's behavior over its explicit type.

This approach allows for more reusable and adaptable code, as seen in the seamless operation of Python's min and max functions.

By focusing on the presence of necessary methods and attributes, duck typing promotes a more intuitive and efficient coding style, ultimately leading to cleaner and more maintainable codebases.

Embracing duck typing can enhance your programming skills, making you a more versatile and effective developer in dynamically typed languages like Python.

Top comments (2)

Collapse
 
sreno77 profile image
Scott Reno

Good and simple explanation

Collapse
 
rafaeljohn9 profile image
JohnKagunda

Thanks, glad you liked it