Isinstance() Function In Python

Overview:

  • 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:  

# 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:

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:

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:

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:

# 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:

stylus is an instance of WritingInstrument: True

Example 4 - Checking an object is an instance of an indirect base class:

class MechanicalPencil(Pencil):
    def write(self):
        print("Mechanical pencil writing")

mechPencil = MechanicalPencil()
print("mechPencil is an instance of WritingInstrument:%s"%isinstance(mechPencil, WritingInstrument))

Output:

A digital pen is a fountain pen:False

mechPencil is an instance of WritingInstrument:True

 


Copyright 2020 © pythontic.com