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

前端 未结 21 2207
太阳男子
太阳男子 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:05

    The best solution I've found so far:

    hasattr(obj, '__contains__')

    which basically checks if the object implements the in operator.

    Advantages (none of the other solutions has all three):

    • it is an expression (works as a lambda, as opposed to the try...except variant)
    • it is (should be) implemented by all iterables, including strings (as opposed to __iter__)
    • works on any Python >= 2.5

    Notes:

    • the Python philosophy of "ask for forgiveness, not permission" doesn't work well when e.g. in a list you have both iterables and non-iterables and you need to treat each element differently according to it's type (treating iterables on try and non-iterables on except would work, but it would look butt-ugly and misleading)
    • solutions to this problem which attempt to actually iterate over the object (e.g. [x for x in obj]) to check if it's iterable may induce significant performance penalties for large iterables (especially if you just need the first few elements of the iterable, for example) and should be avoided

提交回复
热议问题