Python判断一个对象是否可迭代

需要使用到collections模块的Iterable类型。

具体判断方法如下:

>>> from collections import Iterable
>>> isinstance('abc', Iterable)    # str是否可迭代
True
>>> isinstance([1,2,3], Iterable)  # list是否可迭代
True
>>> isinstance(123, Iterable)      # 整数是否可迭代
False</pre>

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha Code