You may have heard that in Python type checking is frowned upon and "duck typing" is embraced instead.
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!
isinstance function can actually be a trusty tool in the land of duck typing.
First, let's review what duck typing means in Python.
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.
This is a premium topic available only to active Python Morsels subscribers.
You can find free reading material on the topics page.
Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.