A Friendly Guide to Duck Typing in Python
Many new Python programmers struggle with the concept of Duck Typing so let's talk about it.
But before then, let's look at why Python is a dynamically typed language. What exactly is dynamic typing:
With dynamic typing, you don't have to strictly declare the type of an object (instance or variable) at creation time
In Python, creating a variable and an instance works like this:
foo = "Hello, world!" bar = Bar() # let's assume we have a Bar() class somewhere
In the above code, Python dynamically knows the type of object we are creating, and we don't have to declare it explicitly.
In some languages like Dart, we have to declare the type of the object during creation time like this:
String foo = "Hello, world!" Bar bar = new Bar() # let's assume we have a Bar() class
Now that we understand dynamic typing. Let's talk about Duck typing and what it means.
Ducks Quack. Parrot's Don't
Ever came across the quote
If it walks like a duck and it quacks like a duck, then it must be a duck
What does it mean?
With our understanding of dynamic typing, let's see how this works in Python. Imagine we have a
# birds.py module class Duck: def swim(self): return 'Duck is swimming...' class Swan: def swim(self): return 'Swan is swimming...' class Parrot: def walk(self): #well, parrots don't swim return 'Parrot is walking...'
...and we decided to import it into our code. We can try to access the behavior of its classes as follows:
for obj in Duck(), Swan(), Parrot(): print(obj.swim()) # Duck is swimming... # Swan is swimming... # AttributeError: 'Parrot' object has no attribute 'swim'
Why would we do that? We know these classes provide similar behavior. We can confidently access them the way we did. This is duck typing.
In a nutshell
Duck typing is accessing a specific expected behavior from a similar set of classes and handling any error at run time.
Why is this important?
I will argue that this is a language construct that influences how developers code.
Duck Typing is possible with dynamically typed languages like Python, which offer us:
Clutter-free Code: Because you don't need to declare distinct types across your code, it reduces the volume of code you write. It also makes it easier to read.
Increased Productivity: It allows us to get things done faster, unlike statically typed language.
Quick Prototyping: Languages are a means to an end. Dynamic languages focus on getting there faster.
But they are not without quirks. Here are some drawbacks:
Possibility of Bad Design: Dynamic languages are not strict and thus loosely opinionated. It gives room for bad designs.
Slower Interpretation Time: Unlike statically typed languages, you have to "Leap before you Look." Static languages compile and give quick feedback while you write code.
Possibility of unmaintainable code: Fast prototyping comes with higher chances of bugs. They offer short-term benefits except when built with well-constructed test-cases.
So, that's all there is to it.
I hope you found this article helpful. Please share. Feel free to share your opinions in the comment section below.