I think, it is a kind of “__main__ vs. module_name” discrepancy -I hope I am using the right word-. I have tried to modify your code: test.py [...] if __name__=="__main__": # is dev object an instance of a PythonDeveloper Class print(isinstance(dev, PythonDeveloper)) print(dev.__cl… Answer from sandraC on discuss.python.org
🌐
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 ...
Discussions

Isinstance() not working as expected when passing objects as arguments to functions (Inheretance)
When working on a project I stumbled upon a problem with isinstance(). I have made a simple example to ilustrate it: test.py import testAux class Developer(object): # Constructor def __init__(self, name): self.name = name class PythonDeveloper(Developer): # Constructor def __init__(self, name, ... More on discuss.python.org
🌐 discuss.python.org
2
0
May 13, 2022
python - What are the differences between type() and isinstance()? - Stack Overflow
To summarize the contents of other ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
How to properly use python's isinstance() to check if a variable is a number? - Stack Overflow
I found some old Python code that was doing something like: if type(var) is type(1): ... As expected, pep8 complains about this recommending usage of isinstance(). Now, the problem is that the More on stackoverflow.com
🌐 stackoverflow.com
What's the canonical way to check for type in Python? - Stack Overflow
As it is, it seems to validate the assumption that this is a "canonical thing to do in Python", which it isn't. 2014-12-11T20:54:17.657Z+00:00 ... What's the difference between instance and "exactly"? If type(a) is Object then isn't it also true that isinstance(a, Object). More on stackoverflow.com
🌐 stackoverflow.com
People also ask

When is it useful to use “isinstance()” in Python?
isinstance()” is useful when you want to perform different actions based on the type of an object or when you need to ensure that a variable contains a specific type of data, such as strings, dictionaries, or custom objects. “ isinstance() ” is useful when you want to perform different actions based on the type of an object or when you need to ensure that a variable contains a specific type of data, such as strings, dictionaries, or custom objects.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › isinstance() in python
Python isinstance(): Mastering Type Checking
What is the purpose of the “isinstance()” method in Python?
The “isinstance()” method is used to check if an object is an instance of a specific class or its subclasses. It helps in type-checking and ensures that your code works with the expected data types. The “ isinstance() ” method is used to check if an object is an instance of a specific class or its subclasses. It helps in type-checking and ensures that your code works with the expected data types.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › isinstance() in python
Python isinstance(): Mastering Type Checking
How does “isinstance()” differ from “type()” in Python?
isinstance()” checks if an object is an instance of a specific class or its subclasses and handles inheritance. “type()” returns the exact type of an object but does not handle inheritance. It only returns the exact type of the object. “ isinstance() ” checks if an object is an instance of a specific class or its subclasses and handles inheritance. “ type() ” returns the exact type of an object but does not handle inheritance. It only returns the exact type of the object.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › isinstance() in python
Python isinstance(): Mastering Type Checking
🌐
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.
🌐
Switowski
switowski.com › blog › type-vs-isinstance
type() vs. isinstance()
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:
Find elsewhere
🌐
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, ...
🌐
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
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › isinstance() in python
Python isinstance(): Mastering Type Checking
November 12, 2024 - Python's "isinstance()" method is an effective tool for type checking in the language. Its main objective is to determine whether an object belongs to that class or a subclass derived from it.
🌐
Sarthaks eConnect
sarthaks.com › 3486182 › python-isinstance-function
Python isinstance() Function
Join Sarthaks live online classes for 7-12, CBSE, State Board, JEE & NEET courses led by experienced expert teachers. Learn, Practice Test, Analyse and ace your exam.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get and Check the Type of an Object in Python - nkmk note
April 22, 2025 - Use type() or isinstance() to check whether an object is of a specific type.
🌐
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).
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?

🌐
ZetCode
zetcode.com › python › isinstance-builtin
Python isinstance Function - Complete Guide
This comprehensive guide explores Python's isinstance function, which checks if an object is an instance of a class or tuple of classes.
🌐
Python Tutorial
techkubo.com › home › python isinstance: checking variable types
Python isinstance: Checking Variable Types - Python Tutorial
June 20, 2025 - Python Tutorial · Main Menu · TechKuboTechKubo · The isinstance function checks if a variable is an instance of a specified type or class. It returns True if the variable matches the type and False otherwise. isinstance is useful for ensuring data types, which is valuable in functions that ...
🌐
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 ...
🌐
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.
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
🌐
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.