Python中判断变量是否可迭代


发布日期 : 2017-11-04 13:00:14 UTC

访问量: 227 次浏览

方法一: 适用于Python2和Python3:

>>> from collections import Iterable
>>> isinstance("str", Iterable)
True

方法二:

只适用于Python3

>>> s = "hello world"
>>> hasattr(s, "__iter__")