Python isinstance
时间: 2023-12-25 17:12:19 浏览: 94
( ) 与 type( ) 有什么区别?
Python isinstance() 和 type() 在判断变量类型时有所不同。 isinstance() 函数判断一个对象是否是已知类型的实例,是则返回 True,否则返回 False。 type() 函数返回的是该变量的数据类型(比如 list、tuple、dictionary 等)。
相关问题
python isinstance
Python中的isinstance函数用于检查一个对象是否是指定类或类型的实例。它的语法是isinstance(object, classinfo),其中object是要检查的对象,classinfo可以是一个类对象、类型对象或由它们组成的元组。如果object是classinfo的实例,则返回True,否则返回False。
Python isinstance
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
```
阅读全文