Goose typing PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
8 min. read Python 3.8—3.12
Tags

You may have heard that in Python type checking is frowned upon and "duck typing" is embraced instead. Python's isinstance function might seem like a tool that runs against our tendency for duck typing. After all, the isinstance function checks types and if we're type checking then we aren't duck typing, right?

Incorrect! Interestingly, we can actually use isinstance and practice duck typing at the same time! Python's isinstance function can actually be a trusty tool in the land of duck typing.

First, let's review what duck typing means in Python.

Duck typing

In Python we tend to care about the behavior of objects much more than the type of objects.

For example the built-in sum function accepts any iterable of numbers.

...

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.