Python isinstance
时间: 2023-10-14 08:26:38 浏览: 98
The Python `isinstance()` function is used to check if an object is an instance of a specified class or any of its derived classes. The function takes two parameters: the object to be checked and the class or tuple of classes to check against.
Syntax:
```
isinstance(object, classinfo)
```
Parameters:
- object: The object to be checked.
- classinfo: A class or a tuple of classes to check against.
Return Value:
- True: If the object is an instance of the specified class or any of its derived classes.
- False: If the object is not an instance of the specified class or any of its derived classes.
Example:
```python
class Person:
pass
class Student(Person):
pass
s = Student()
print(isinstance(s, Student)) # Output: True
print(isinstance(s, Person)) # Output: True
print(isinstance(s, object)) # Output: True
print(isinstance(s, str)) # Output: False
```
阅读全文