Python isinstance()

Python’s built-in isinstance(object, class) function takes an object and a class as input arguments. It returns True if the object is an instance of the class. Otherwise, it returns False. Instead of a class, you can also pass a tuple of classes to check if the object is an instance of any of the classes in the tuple—such as in isinstance(object, (class_A, class_B, ...)).

Python isinstance()

Usage

Learn by example! Here’s an example on how to use the isinstance() built-in function.

>>> isinstance(42, int)
True
>>> isinstance(42, (float, list, int))
True
>>> isinstance('42', (float, list, int))
False

Video isinstance()

Syntax isinstance()

The isinstance() method has the following syntax:

Syntax: 
isinstance(object, class)         # Check if object is instance of class
isinstance(object, (class_A, class_B, ...))         # Check if object is instance of any of the classes in the tuple
ArgumentsobjectObject that should be checked for class membership.
class or tuple of classesClass or tuple of classes this object may or may not belong to.
Return ValueBooleanReturns whether the object belongs to the class (or the classes) or not.

Return value from isinstance()

The isinstance(object, class) method returns True if the object is an instance of the class. Otherwise, it returns False.

Interactive Shell Exercise: Understanding isinstance()

Consider the following interactive code:

Exercise: Guess the output of this code snippet. Only then execute and test if you were right!


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


Summary

Python’s built-in isinstance(object, class) function takes an object and a class as input arguments.

  • It returns True if the object is an instance of the class.
  • Otherwise, it returns False.
>>> isinstance(42, int)
True
>>> isinstance(42, float)
False

Instead of a class, you can also pass a tuple of classes to check if the object is an instance of any of the classes in the tuple—such as in isinstance(object, (class_A, class_B, ...)).

>>> isinstance(42, (float, list, int))
True

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!