Python isinstance()
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. This check is useful for type validation in functions, debugging, and ensuring safe operations on data structures.
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 hours
Syntax
isinstance(object, class)
Parameters:
object: The object, or a reference to the object, to be tested.class: The type to be used by the function in this assertion (e.g.list,set, etc.).
Return value:
If the given object is of the designated value type, the isinstance() function returns True, otherwise it returns False.
Example 1: Basic Usage of isinstance()
This example uses the isinstance() function to check if the given value is a string:
value ="Hello World!"ifisinstance(value,str):print("String")else:print("Not a string")Copy to clipboardCopy to clipboard
Here is the output:
StringCopy to clipboardCopy to clipboard
Example 2: Checking Against Multiple Types Using isinstance()
This example uses the isinstance() function to check if the given value is a list or tuple:
data =[1,2,3]ifisinstance(data,(list,tuple)):print("List or tuple")else:print("Not a list or tuple")Copy to clipboardCopy to clipboard
Here is the output:
List or tupleCopy to clipboardCopy to clipboard
Codebyte Example: Custom Class Type Checking Using isinstance()
This codebyte example uses the isinstance() function to check if the given object belongs to a user-defined class:
Visit usVisit usCodeHide codeOutputHide outputCopy to your clipboard
Since the given object pet belongs to the user-defined class Dog, the code will return True.
Frequently Asked Questions
1. What is the difference between isinstance() and type()?
type() checks for the exact type and does not consider inheritance, whereas isinstance() is preferred when you want to account for subclasses.
2. Is isinstance() faster or slower than type()?
Generally, type() is faster because itβs a simpler check, but isinstance() is more flexible and recommended in most cases due to its support for inheritance.
3. Can isinstance() be used with abstract base classes?
Yes, isinstance() can be used with abstract base classes. This is common when working with interfaces or abstract classes using the abc module.
All contributors
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 hours
