我还不太熟悉 Python 的 OOP 方面,所以这有点超出我的理解范围。
>>> a=[[2,3,4]]
>>> types = (list,str,int,str,int)
>>> b=isinstance(a,types)
True
无论我在列表类型后添加什么类型,表达式总是返回 True。
我读过 isinstance() 方法接受派生类类型,但我无法真正说服自己我知道这里发生了什么。
这也返回 true。是因为 3 个都属于同一个类吗?
>>> isinstance([], (tuple, list, set))
True
请您参考如下方法:
In [1]: isinstance?Docstring:
isinstance(object, class-or-type-or-tuple) -> boolReturn whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object's type. The form using a tuple,
isinstance(x, (A, B, ...)), is a shortcut for:isinstance(x, A) or isinstance(x, B) or ... (etc.).Type:
builtin_function_or_method
所以,它实际上是一个或语句。很清楚,对吧?
