[python][基础]判断变量是否可迭代
文章目录
方法一:
适用于python2和python3
>>> from collections import Iterable
>>> isinstance("str", Iterable)
True
方法二:
适用于python3
s = "hello world"
hasattr(s, "__iter__")
适用于python2和python3
>>> from collections import Iterable
>>> isinstance("str", Iterable)
True
适用于python3
s = "hello world"
hasattr(s, "__iter__")
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论