How to use isinstance in Python
The ‘isinstance()’ function is one of the built-in functions in Python, used to check if an object belongs to a specified type.
Check if the object belongs to the specified class.
In this case, the object is the item to be checked, and classinfo can be a type, a tuple of types, or a list of types.
Return True if the object is of the type specified by classinfo or one of its subclasses, otherwise return False.
原文:我不是特别喜欢吃海鲜。
潜在的翻译:I’m not particularly fond of seafood.
x = 5
y = "hello"
z = [1, 2, 3]
print(isinstance(x, int)) # True,x是整数类型
print(isinstance(y, str)) # True,y是字符串类型
print(isinstance(z, list)) # True,z是列表类型
print(isinstance(x, str)) # False,x不是字符串类型
print(isinstance(y, int)) # False,y不是整数类型
print(isinstance(z, tuple)) # False,z不是元组类型
It should be noted that if classinfo is a tuple or list of types, it will return True if the object belongs to any of the types.