Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
isinstance() method - Python
Next article icon

isinstance() method - Python

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 ensuring safe operations in dynamic code. Example:

Python
x = 10 print(isinstance(x, int))  # x is int  class Animal: pass dog = Animal() print(isinstance(dog, Animal))  # dog is Animal 

Output
True True 

Explanation:

  • isinstance(x, int) returns True as x is an integer.
  • isinstance(dog, Animal) returns True as dog is an instance of the Animal class.

Syntax of isinstance()

isinstance(obj, classinfo)

Parameters:

  • obj: The object to check.
  • classinfo: A class, type or a tuple of classes/types.

Returns:

  • True if obj is an instance of classinfo or any type in the tuple and otherwise False.

Raises: TypeError if classinfo is not a valid class or type.

Examples of isinstance()

Example 1: In this example, we use isinstance() to check if an integer and a list are instances of the int or str types.

Python
a = 5 b = [10,20,37]  print(isinstance(a, int))   print(isinstance(b, list))  print(isinstance(b, (int, list,str)))  

Output
True True True 

Explanation:

  • isinstance(a, int) returns True because a is an integer.
  • isinstance(b, list) returns True because b is a list.
  • isinstance(b, (int, list, str)) returns True because b is a list, which is one of the types in the tuple.

Example 2: In this example, we check if an object is an instance of a specific class or its parent class.

Python
class Animal: pass class Dog(Animal): pass  dog = Dog() print(isinstance(dog, Dog))   print(isinstance(dog, Animal))   

Output
True True 

Explanation:

  • isinstance(dog, Dog) returns True as dog is an instance of the Dog class.
  • isinstance(dog, Animal) returns True as dog is also an instance of the parent class Animal.

Example 3: In this example, we check if an object is an instance of a specific type, such as a Python string or a dictionary.

Python
a = "Hello" print(isinstance(a, str))  b = {"apple": 1} print(isinstance(b, dict)) 

Output
True True 

Explanation:

  • isinstance(a, str) returns True as a is a string.
  • isinstance(b, dict) returns True as b is a dictionary.

Example 4: In this example, we check if an object is an instance of a class or its derived class.

Python
class MyClass:     def method(self): return "Hello"  obj = MyClass() print(isinstance(obj.method(), str))  

Output
True 

Explanation: isinstance(obj.method(), str) returns True as obj.method() returns the string "Hello".

Difference between isinstance() and type() methods in Python

The following table highlights the differences between the isinstance() and type() methods, both used for type checking. Knowing when to use each helps write more efficient and reliable code.

isinstance()

type()

Syntax: isinstance(object, class)Syntax: type(object)

It checks if an object is of a specific class type

It returns the class type of an object

It can check if the object belongs to a class and its subclasses

It cannot deal with inheritance

It is faster as compared to type()It is slower than isinstance()
It returns either True or FalseIt returns the type of the object
It can check for multiple classes at a timeIt cannot do this
Example: isinstance(10, (int, str))Example: type(10)

Next Article
isinstance() method - Python

M

manjeet_04
Improve
Article Tags :
  • Python
  • Python-Built-in-functions
  • python
Practice Tags :
  • python
  • python

Similar Reads

    Instance method in Python
    A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to
    2 min read
    Python List methods
    Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, we’ll explore all Python list methods with a simple example.List MethodsLet's look at different list methods in Python:append(): Adds an
    3 min read
    Python Dictionary get() Method
    Python Dictionary get() Method returns the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).Python Dictionary get() Method Syntax:Syntax : Dict.get(key, Value)Parameters: key: The key name of the item you want to return
    3 min read
    Python Dictionary Methods
    Python dictionary methods is collection of Python functions that operates on Dictionary.Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the fu
    5 min read
    Python __len__() magic method
    Python __len__ is one of the various magic methods in Python programming language, it is basically used to implement the len() function in Python because whenever we call the len() function then internally __len__ magic method is called. It finally returns an integer value that is greater than or eq
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences