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