The isinstance() built-in function checks whether an object is one of the given types. The function returns True for instances of direct, indirect and virtual subclasses.
The built-in function isinstance() checks whether an object is an instance of one or more given types and returns True if the object is an instance of the one of the types given. It returns False, if the instance is not of any of the types given.
For example, a digital pen can be classified as both a writing instrument and a digital pen. An eraser can neither be classified as a writing instrument nor a digital pen.
Example 1
python
# Example Python program that checks
# an object is of a specific type
class WritingInstrument:
def write(self):
print("writing")
class FountainPen(WritingInstrument):
def write(self):
print("FountainPen writing")
class Pencil(WritingInstrument):
def write(self):
print("Pencil writing")
# Create a FountainPen instance
pen = FountainPen()
pen.write()
isWriter = isinstance(pen, Pencil)
print("A pen is a pencil:%s"%isWriter)
isWriter = isinstance(pen, FountainPen)
print("A fountain pen is a FountainPen:%s"%isWriter)
isWriter = isinstance(pen, WritingInstrument)
print("A pen is a writing instrument:%s"%isWriter)
Output
Output
FountainPen writing
A pen is a pencil:False
A fountain pen is a FountainPen:True
A pen is a writing instrument:True
Example 2 - Checking an object is an instance of more than one type
python
class DigitalPen(WritingInstrument):
def write(self):
print("Digital pen writing")
digiPen = DigitalPen()
isWriter = isinstance(digiPen, (DigitalPen, WritingInstrument))
print("A digital pen is a digital pen and also an writing instrument:%s"%isWriter)
isWriter = isinstance(digiPen, FountainPen)
print("A digital pen is a fountain pen:%s"%isWriter)
Output
Output
A digital pen is a digital pen and also an writing instrument:True
A digital pen is a fountain pen:False
Example 3 - Checking an object is an instance of virtual base class
python
# Example Python program that instantiates a virtual subclass
# and tests whether it is an instance of a virtual base class
from abc import ABC
from abc import abstractmethod
# WritingInstrument interface
class WritingInstrument(ABC):
@abstractmethod
def write(self):
pass
# Independent DeviceSpecificStylus not implementing the
# interface WritingInstrument
class DeviceSpecificStylus:
def write(self):
print("DeviceSpecificStylus moving")
# Developer decides DeviceSpecificStylus is a perfect
# WritingInstrument and registers it with the WritingInstrument
# hierarchy
WritingInstrument.register(DeviceSpecificStylus)
stylus = DeviceSpecificStylus()
isWriter = isinstance(stylus, WritingInstrument)
print("stylus is an instance of WritingInstrument: %s"%isWriter)
Output
Output
stylus is an instance of WritingInstrument: True
Example 4 - Checking an object is an instance of an indirect base class
python
class MechanicalPencil(Pencil):
def write(self):
print("Mechanical pencil writing")
mechPencil = MechanicalPencil()
print("mechPencil is an instance of WritingInstrument:%s"%isinstance(mechPencil, WritingInstrument))
Output
Output
A digital pen is a fountain pen:False
mechPencil is an instance of WritingInstrument:True
Your privacy choices
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on