在以下代码段中:
In [1]: x = [0]
In [2]: isinstance(x, list)
Out[2]: True
In [3]: isinstance(x, (list, set))
Out[3]: True
In [4]: isinstance(x, [list, set])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-95dd12d6777a> in <module>()
----> 1 isinstance(x, [list, set])
TypeError: isinstance() arg 2 must be a type or tuple of types
为什么isinstance
中的[4]
坚持认为第二个参数是元组,而不仅仅是可迭代的(例如list
或set
)?似乎是一个奇怪的设计决定。