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

如何判断一个对象是可迭代对象? 
方法是通过collections模块的Iterable类型判断:

 

 

>>> from collections import Iterable
>>> isinstance('abc',Iterable)
True
>>> isinstance([1,2,3,4],Iterable)
True
>>> isinstance(1234,Iterable)
False
>>> isinstance((1,),Iterable)
True
>>> L = ['a','b','c']
>>> enumerate(L)
<enumerate object at 0x03AA94E0>
>>> isinstance(enumerate(L),Iterable)
True
>>> for m,n in enumerate(L):
...     print m,n 
... 
0 a
1 b
2 c

 

posted on 2017-05-21 12:49  星河赵  阅读(7195)  评论(0编辑  收藏  举报

导航