python如何判断对象是否iterable

来源:互联网 发布:python人脸识别闪退 编辑:程序博客网 时间:2024/04/28 22:34

1 通常判断

hasattr(myObj, '__iter__')

2,

try:    some_object_iterator = iter(some_object)except TypeError, te:    print some_object, 'is not iterable'

3,

try:   _ = (e for e in my_object)except TypeError:   print my_object, 'is not iterable'

4

import collectionsif isinstance(e, collections.Iterable):    # e is iterable

0 0