How to use Duck Typing in Python

Share This Post

You’ll learn about duck typing in this lesson. This expression was originated by the statement “If it looks like a duck and talks like a duck, that must be a duck.”. (Other versions exist.)

Duck typing is a dynamic typing concept in which an object’s type or class is less essential than the methods it defines. You don’t verify types at all when you use duck typing. Rather, you look for the existence of a specific function or trait.

Way to use Duck Typing in Python

You may call len() on any Python class that has a. len () method, for example:

class TheHobbit:

    def __len__(self):

        return 95022

the_hobbit = TheHobbit()




my_str = "Hello World"

my_list = [34, 54, 65, 78]

my_dict = {"one": 123, "two": 456, "three": 789}









my_int = 7

my_float = 42.3


Traceback (most recent call last):

File "", line 1, in



Traceback (most recent call last):

File "", line 1, in


The object of type ‘float’ has no len, which is a TypeError ()

The only actual requirement on obj is that it must define a. len () method before you may call len(obj). Otherwise, the object might be of any type, including str, list, dict, and TheHobbit.

When it comes to typing in Python, another term that comes up is duck typing. Its title was inspired by the statement “If it quacks like a duck & sounds like a duck, that must be a duck.”

Duck typing is a sort of dynamic typing in which the type of an object’s class is less relevant than the methods it specifies.

Rather of looking for the class or type, you’ll look for specific methods or properties that that object contains. You can use the length function, len(), on any Python object that has a. len() (dunder length) method, for example.

I’ll show you how to do this by creating a class called TheHobbit. The Hobbit defines the length method. len () to return an integer of 95022, the book’s word count, or length. As a result, if you create a new object using that class,

When you call the hobbit’s length method, len(), it returns the value 95022. The return value of the. len () function return calling len().

In truth, len(implementation )’s is substantially the same as the following.

If you define len(), it returns the. len () method of the object.

To emphasize this, if you have code that wants to call an object’s len() method, the object’s only actual requirement is that it must define a length method, the. len () method.

Otherwise, that object could be a string, a list, a dictionary, or your freshly declared class TheHobbit, all of which would quack like a duck.

So, if you had a couple of additional objects, say my int and my float, that is of the kinds int (integer) and float, and you tried to call the len() method on them, you’d find that in the case of my int, it raises a TypeError, because the object type int has no len() function. And the same goes for

There is no len() method on a floating object. As a result, these are birds of a separate species. It’s time to go into type hinting in the following video.


Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img