How do I check if a value is an instance of a specific class in Python?
Davide S
davide s profile pic

To check if a value is an instance of a specific class in Python, you can use theisinstance() function. Theisinstance() function allows you to determine if an object belongs to a particular class or its subclasses. Here's an example of how you can perform this check:

1
2
3
4
5
6
7
8
9
10
11
12

class MyClass:
    pass

class MySubClass(MyClass):
    pass

obj1 = MyClass()
obj2 = MySubClass()

print(isinstance(obj1, MyClass))  # Output: True
print(isinstance(obj2, MyClass))  # Output: True

In the above code, theisinstance() function is used to check ifobj1 andobj2 are instances of theMyClass class. Bothobj1 andobj2 are considered instances ofMyClass becauseMySubClass is a subclass ofMyClass. You can use theisinstance() function by providing the object and the class (or tuple of classes) to check against as arguments. It will returnTrue if the object is an instance of the specified class or any of its subclasses. Otherwise, it will returnFalse. You can also useisinstance() to check against multiple classes by providing a tuple of classes as the second argument:

1
2
3
4
5
6
7
8
9
10

class MyClass:
    pass

class MyOtherClass:
    pass

obj = MyClass()

print(isinstance(obj, (MyClass, MyOtherClass)))  # Output: True

In the above code,isinstance() is used to check ifobj is an instance of eitherMyClass orMyOtherClass. Sinceobj is an instance ofMyClass, the check returnsTrue. Theisinstance() function is useful when you need to determine the type of an object dynamically or perform conditional checks based on the object's class. It allows you to write flexible and reusable code that can handle different object types appropriately.

Similar Questions

How do I check if a value is an instance of a specific class or any of its parent classes in Python?

How do I check if a variable is an instance of a specific class or its subclasses in Python?

How do I check if a value is an instance of any of multiple classes in Python?

How can I check if a value is a subclass of a specific class in Python?

How do I check if a value is an instance of a specific class or any of its parent classes in Python using a loop?

How can I check if a list contains a specific value in Python?

How do I check if a value is a subclass of a specific class or its subclasses in Python?

How do I check if a value is an instance of any of multiple classes or their subclasses in Python?

How do I check if a value is a string in Python?

How do I check if a value is an instance of a specific class or any of its parent classes in Python using issubclass() function?

How do I check if a value is an integer in Python?

How do I check if a value is an instance of a specific class or any of its parent classes in Python using a conditional expression?

How do I check if a variable is of a specific type in Python?

How do I check if a value is in a list in Python?

How do I check if a value is a substring of a string in Python?

How do I check if a value is an instance of a specific class or any of its parent classes in Python using isinstance() and recursion?

How do I check if a value is a list in Python?

How do I check if a value is a boolean in Python?

How do I check if a value is a function in Python?

How do I check if a value is a dictionary with a specific key in Python?