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!


Check out my 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).

Publisher 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!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!