在线工具 在线编程 在线白板 在线工具 在线编程 在线白板

python isinstance()

请说下,python isinstance()
最新回答
可爱的狗蛋

2025-03-26 08:39:09

isinstance() 函数在Python中的作用是判断一个对象是否属于某个已知的类型。

isinstance() 函数在Python官方文档中的解释如下:

isinstance(object, classinfo)

Return True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtual) subclass thereof. If object is not an object of the given type, the function always returns False. If classinfo is a tuple of type objects (or recursively, other such tuples) or a Union Type of multiple types, return True if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

与type()的区别在于,isinstance()不仅可以检查对象的类型,还可以检查对象的子类。而type()只能检查对象的类型。

isinstance() 实例参考文献:

Built-in Functions — Python 3.10.0 documentation

Python isinstance() 函数 | 菜鸟教程 (runoob.com)