Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Duck Typing

In this lesson, you’ll learn about duck typing. This term comes from the saying “If it walks like a duck, and it quacks like a duck, then it must be a duck.” (There are other variations).

Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. When you use duck typing, you do not check types at all. Instead, you check for the presence of a given method or attribute.

For example, you can call len() on any Python object that defines a .__len__() method:

Python
>>> class TheHobbit:
...     def __len__(self):
...         return 95022
...
...
>>> the_hobbit = TheHobbit()

>>> the_hobbit
<__main__.TheHobbit object at 0x108deeef0>

>>> len(the_hobbit)
95022

>>> my_str = "Hello World"
>>> my_list = [34, 54, 65, 78]
>>> my_dict = {"one": 123, "two": 456, "three": 789}

>>> len(my_str)
11
>>> len(my_list)
4
>>> len(my_dict)
3
>>> len(the_hobbit)
95022

>>> my_int = 7
>>> my_float = 42.3

>>> len(my_int)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    len(my_int)
TypeError: object of type 'int' has no len()

>>> len(my_float)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    len(my_float)
TypeError: object of type 'float' has no len()

In order for you to call len(obj), the only real constraint on obj is that it must define a .__len__() method. Otherwise, the object can be of types as different as str, list, dict, or TheHobbit.

00:00 Another term that comes up when speaking about typing inside of Python is duck typing. The name comes from the phrase, “If it walks like a duck and it quacks like a duck, then it must be a duck.” Duck typing is related to dynamic typing, where the type of the class of an object is going to be less important than the methods that it defines.

00:23 Instead of checking for the class or the type, you’re going to check for specific methods or attributes that that object has. As an example, you can call the function length, len(), on any Python object that defines a .__len__() (dunder length) method.

00:43 I’ll have you try this out by defining a class called TheHobbit. TheHobbit defines the .__len__() length method to return an integer of 95022, which is the book’s word count, or the length. So if you make a new object from that class,

01:14 if you were to run the method length, len(), on the_hobbit, it returns the value 95022. Note that the call to len() gives the return value of the .__len__() method.

01:31 In fact, the implementation of len() is essentially equivalent to the following.

01:39 If you were to define len(), it returns the object’s .__len__() method.

01:48 So, to reinforce this, if you have code that wants to call the len() method on an object, the only real constraint that there is on the object is that it must define a length method, the .__len__() method.

02:05 Otherwise, that object could be of types as different as a string, a list, a dictionary, or your newly-defined class TheHobbit—all quacking like a duck.

02:21 So, if you had a couple of other objects, like my_int and my_float, that are of the types of int (integer) and float, and you tried to call the len() method on those, in the case of my_int, you’d see here that it raises an exception of a TypeError, the object type of int has no len() method. And the same thing with my_float.

02:46 The object of type float has no len() method. So these are birds of a different type. In the next video, it’s time to dive into type hinting.

Become a Member to join the conversation.