To summarize the contents of other (already good!) answers, isinstance caters for inheritance (an instance of a derived class is an instance of a base class, too), while checking for equality of type does not (it demands identity of types and rejects instances of subtypes, AKA subclasses).

Normally, in Python, you want your code to support inheritance, of course (since inheritance is so handy, it would be bad to stop code using yours from using it!), so isinstance is less bad than checking identity of types because it seamlessly supports inheritance.

It's not that isinstance is good, mind you—it's just less bad than checking equality of types. The normal, Pythonic, preferred solution is almost invariably "duck typing": try using the argument as if it was of a certain desired type, do it in a try/except statement catching all exceptions that could arise if the argument was not in fact of that type (or any other type nicely duck-mimicking it;-), and in the except clause, try something else (using the argument "as if" it was of some other type).

basestring is, however, quite a special case—a builtin type that exists only to let you use isinstance (both str and unicode subclass basestring). Strings are sequences (you could loop over them, index them, slice them, ...), but you generally want to treat them as "scalar" types—it's somewhat incovenient (but a reasonably frequent use case) to treat all kinds of strings (and maybe other scalar types, i.e., ones you can't loop on) one way, all containers (lists, sets, dicts, ...) in another way, and basestring plus isinstance helps you do that—the overall structure of this idiom is something like:

if isinstance(x, basestring)
  return treatasscalar(x)
try:
  return treatasiter(iter(x))
except TypeError:
  return treatasscalar(x)

You could say that basestring is an Abstract Base Class ("ABC")—it offers no concrete functionality to subclasses, but rather exists as a "marker", mainly for use with isinstance. The concept is obviously a growing one in Python, since PEP 3119, which introduces a generalization of it, was accepted and has been implemented starting with Python 2.6 and 3.0.

The PEP makes it clear that, while ABCs can often substitute for duck typing, there is generally no big pressure to do that (see here). ABCs as implemented in recent Python versions do however offer extra goodies: isinstance (and issubclass) can now mean more than just "[an instance of] a derived class" (in particular, any class can be "registered" with an ABC so that it will show as a subclass, and its instances as instances of the ABC); and ABCs can also offer extra convenience to actual subclasses in a very natural way via Template Method design pattern applications (see here and here [[part II]] for more on the TM DP, in general and specifically in Python, independent of ABCs).

For the underlying mechanics of ABC support as offered in Python 2.6, see here; for their 3.1 version, very similar, see here. In both versions, standard library module collections (that's the 3.1 version—for the very similar 2.6 version, see here) offers several useful ABCs.

For the purpose of this answer, the key thing to retain about ABCs (beyond an arguably more natural placement for TM DP functionality, compared to the classic Python alternative of mixin classes such as UserDict.DictMixin) is that they make isinstance (and issubclass) much more attractive and pervasive (in Python 2.6 and going forward) than they used to be (in 2.5 and before), and therefore, by contrast, make checking type equality an even worse practice in recent Python versions than it already used to be.

Answer from Alex Martelli on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_func_isinstance.asp
Python isinstance() Function
Python Examples Python Compiler ... Python Training ... The isinstance() function returns True if the specified object is of the specified type, otherwise False....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-isinstance-method
isinstance() method - Python - GeeksforGeeks
July 11, 2025 - isinstance() is a built-in Python function that checks whether an object or variable is an instance of a specified type or class (or a tuple of classes), returning True if it matches and False otherwise, making it useful for type-checking and ...
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.4 documentation
February 27, 2026 - The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
🌐
Real Python
realpython.com › ref › builtin-functions › isinstance
isinstance() | Python’s Built-in Functions – Real Python
The built-in isinstance() function checks if an object is an instance of a specified class or a subclass thereof, returning a Boolean value. It’s a versatile tool for explicit type checking in Python, as it considers subclass relationships:
🌐
Programiz
programiz.com › python-programming › methods › built-in › isinstance
Python isinstance()
Become a certified Python programmer. Try Programiz PRO! ... The isinstance() function checks if the object (first argument) is an instance or subclass of classinfo class (second argument).
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › isinstance.html
isinstance — Python Reference (The Right Way) 0.1 documentation
>>> isinstance(u'foo', (basestring, str, unicode)) True >>> isinstance(u'foo', (basestring, str)) True >>> isinstance(u'foo', (basestring)) True >>> isinstance(u'foo', (str)) False
Top answer
1 of 8
1557

To summarize the contents of other (already good!) answers, isinstance caters for inheritance (an instance of a derived class is an instance of a base class, too), while checking for equality of type does not (it demands identity of types and rejects instances of subtypes, AKA subclasses).

Normally, in Python, you want your code to support inheritance, of course (since inheritance is so handy, it would be bad to stop code using yours from using it!), so isinstance is less bad than checking identity of types because it seamlessly supports inheritance.

It's not that isinstance is good, mind you—it's just less bad than checking equality of types. The normal, Pythonic, preferred solution is almost invariably "duck typing": try using the argument as if it was of a certain desired type, do it in a try/except statement catching all exceptions that could arise if the argument was not in fact of that type (or any other type nicely duck-mimicking it;-), and in the except clause, try something else (using the argument "as if" it was of some other type).

basestring is, however, quite a special case—a builtin type that exists only to let you use isinstance (both str and unicode subclass basestring). Strings are sequences (you could loop over them, index them, slice them, ...), but you generally want to treat them as "scalar" types—it's somewhat incovenient (but a reasonably frequent use case) to treat all kinds of strings (and maybe other scalar types, i.e., ones you can't loop on) one way, all containers (lists, sets, dicts, ...) in another way, and basestring plus isinstance helps you do that—the overall structure of this idiom is something like:

if isinstance(x, basestring)
  return treatasscalar(x)
try:
  return treatasiter(iter(x))
except TypeError:
  return treatasscalar(x)

You could say that basestring is an Abstract Base Class ("ABC")—it offers no concrete functionality to subclasses, but rather exists as a "marker", mainly for use with isinstance. The concept is obviously a growing one in Python, since PEP 3119, which introduces a generalization of it, was accepted and has been implemented starting with Python 2.6 and 3.0.

The PEP makes it clear that, while ABCs can often substitute for duck typing, there is generally no big pressure to do that (see here). ABCs as implemented in recent Python versions do however offer extra goodies: isinstance (and issubclass) can now mean more than just "[an instance of] a derived class" (in particular, any class can be "registered" with an ABC so that it will show as a subclass, and its instances as instances of the ABC); and ABCs can also offer extra convenience to actual subclasses in a very natural way via Template Method design pattern applications (see here and here [[part II]] for more on the TM DP, in general and specifically in Python, independent of ABCs).

For the underlying mechanics of ABC support as offered in Python 2.6, see here; for their 3.1 version, very similar, see here. In both versions, standard library module collections (that's the 3.1 version—for the very similar 2.6 version, see here) offers several useful ABCs.

For the purpose of this answer, the key thing to retain about ABCs (beyond an arguably more natural placement for TM DP functionality, compared to the classic Python alternative of mixin classes such as UserDict.DictMixin) is that they make isinstance (and issubclass) much more attractive and pervasive (in Python 2.6 and going forward) than they used to be (in 2.5 and before), and therefore, by contrast, make checking type equality an even worse practice in recent Python versions than it already used to be.

2 of 8
454

Here's an example where isinstance achieves something that type cannot:

class Vehicle:
    pass

class Truck(Vehicle):
    pass

In this case, a Truck object is a Vehicle, but you'll get this:

isinstance(Vehicle(), Vehicle)  # returns True
type(Vehicle()) == Vehicle      # returns True
isinstance(Truck(), Vehicle)    # returns True
type(Truck()) == Vehicle        # returns False, and this probably won't be what you want.

In other words, isinstance() is true for subclasses, too.

Also see: How to compare type of an object in Python?

🌐
Real Python
realpython.com › what-does-isinstance-do-in-python
What Does isinstance() Do in Python? – Real Python
July 21, 2025 - Python’s isinstance() function helps you determine if an object is an instance of a specified class or its superclass, aiding in writing cleaner and more robust code. You use it to confirm that function parameters are of the expected types, ...
Find elsewhere
🌐
Codecademy
codecademy.com › docs › python › built-in functions › isinstance()
Python | Built-in Functions | isinstance() | Codecademy
May 29, 2025 - In Python, the isinstance() function checks whether an object is an instance of a specified type, class, or a tuple of classes. If it is, the function returns True, otherwise it returns False.
🌐
Switowski
switowski.com › blog › type-vs-isinstance
type() vs. isinstance() - Sebastian Witowski
October 15, 2020 - But the following recommendations are still valid no matter which version of Python you are using. Speed is not the only difference between these two functions. There is actually an important distinction between how they work: type only returns the type of an object (its class). We can use it to check if variable is of a type str. isinstance checks if a given object (first parameter) is:
🌐
Medium
medium.com › @sunilnepali844 › understanding-the-isinstance-function-in-python-9f254be49e0a
Understanding the isinstance() Function in Python | by Sunil Nepali | Medium
August 26, 2024 - The isinstance() function is a built-in Python function that is widely used for type checking. It helps determine if an object is an instance of a particular class or a subclass thereof.
🌐
Hyperskill
hyperskill.org › university › python › isinstance-in-python
Python isinstance()
August 2, 2024 - Use isinstance() to check if an object is an instance of this class.
🌐
Devcuriosity
devcuriosity.com › manual › details › python-isinstance-function
Python - isinstance() function. Check if object is instance of a class
To summarize, isinstance() is a function that allows you to check if an object is an instance of a specific class or a subclass. Checks Class Hierarchy: Returns True for both classes and subclasses of the provided type. Supports Multiple Types: Use a tuple for checking against multiple classes ...
🌐
Vultr Docs
docs.vultr.com › python › built-in › isinstance
Python isinstance() - Check Type Membership | Vultr Docs
September 27, 2024 - The isinstance() function in Python is invaluable for ensuring type correctness in data handling and processing scenarios. By incorporating type checks using isinstance(), you improve the safety and predictability of your Python programs.
Top answer
1 of 4
130

In Python 2, you can use the types module:

>>> import types
>>> var = 1
>>> NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
>>> isinstance(var, NumberTypes)
True

Note the use of a tuple to test against multiple types.

Under the hood, IntType is just an alias for int, etc.:

>>> isinstance(var, (int, long, float, complex))
True

The complex type requires that your python was compiled with support for complex numbers; if you want to guard for this use a try/except block:

>>> try:
...     NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
... except AttributeError:
...     # No support for complex numbers compiled
...     NumberTypes = (types.IntType, types.LongType, types.FloatType)
...

or if you just use the types directly:

>>> try:
...     NumberTypes = (int, long, float, complex)
... except NameError:
...     # No support for complex numbers compiled
...     NumberTypes = (int, long, float)
...

In Python 3 types no longer has any standard type aliases, complex is always enabled and there is no longer a long vs int difference, so in Python 3 always use:

NumberTypes = (int, float, complex)

Last but not least, you can use the numbers.Numbers abstract base type (new in Python 2.6) to also support custom numeric types that don't derive directly from the above types:

>>> import numbers
>>> isinstance(var, numbers.Number)
True

This check also returns True for decimal.Decimal() and fractions.Fraction() objects.

This module does make the assumption that the complex type is enabled; you'll get an import error if it is not.

2 of 4
21

Python 2 supports four types for numbers int,float, long and complexand python 3.x supports 3:int, float and complex

>>> num = 10
>>> if isinstance(num, (int, float, long, complex)): #use tuple if checking against multiple types
      print('yes it is a number')

yes it is a number
>>> isinstance(num, float)   
False
>>> isinstance(num, int)
True
>>> a = complex(1, 2)
>>> isinstance(a, complex)
True
🌐
w3resource
w3resource.com › python › built-in-function › isinstance.php
Python: isinstance() function
The isinstance() function returns true if the object argument is an instance of the classinfo argument, or of a subclass thereof. If object is not an object of the given type, the function always returns false.
🌐
Medium
medium.com › @sim30217 › isinstance-dd8555e600fb
isinstance(). isinstance() is a built-in Python… | by Simsangcheol | Medium
March 23, 2023 - isinstance() is a built-in Python function that is used to check if an object is an instance of a specified class or a subclass of that
🌐
AskPython
askpython.com › home › python isinstance() method
Python isinstance() Method - AskPython
August 6, 2022 - As the name suggests, the Python isinstance() method is an in-built method which checks whether an object is an instance of any particular class or not.
🌐
Python.org
discuss.python.org › python help
How to `isinstance` a typing type so that it works with static type checkers? - Python Help - Discussions on Python.org
January 25, 2024 - My code defines lots of typing types, and I need to isinstance lots of objects against them at runtime. Something like this: Type1 = list[int] Type2 = list[str] # etc. # get data from the outside, e.g. read from a file data = get_data() # NOT WORKING if isinstance(data, Type1): "do something" elif isinstance(data, Type2): "do something else" else: "etc."