In Python, how do I determine if an object is iterable?

前端 未结 21 2212
太阳男子
太阳男子 2020-11-22 00:35

Is there a method like isiterable? The only solution I have found so far is to call

hasattr(myObj, \'__iter__\')

But I am not

21条回答
  •  长情又很酷
    2020-11-22 01:03

    I found a nice solution here:

    isiterable = lambda obj: isinstance(obj, basestring) \
        or getattr(obj, '__iter__', False)
    

提交回复
热议问题