Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

hasattr(myObj, '__iter__') 

But I am not sure how fool-proof this is.

like image 481
willem Avatar asked Dec 23 '09 12:12

willem


People also ask

Which of these objects are iterable in Python?

Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from.

What makes something iterable in Python?

Iterable is an object which can be looped over or iterated over with the help of a for loop. Objects like lists, tuples, sets, dictionaries, strings, etc. are called iterables. In short and simpler terms, iterable is anything that you can loop over.

Is object an iterable?

The iterable protocol Some built-in types are built-in iterables with a default iteration behavior, such as Array or Map , while other types (such as Object ) are not.

Which of the following Python objects is not iterable?

If you are running your Python code and you see the error “TypeError: 'int' object is not iterable”, it means you are trying to loop through an integer or other data type that loops cannot work on. In Python, iterable data are lists, tuples, sets, dictionaries, and so on.


1 Answers

  1. Checking for __iter__ works on sequence types, but it would fail on e.g. strings in Python 2. I would like to know the right answer too, until then, here is one possibility (which would work on strings, too):

    from __future__ import print_function  try:     some_object_iterator = iter(some_object) except TypeError as te:     print(some_object, 'is not iterable') 

    The iter built-in checks for the __iter__ method or in the case of strings the __getitem__ method.

  2. Another general pythonic approach is to assume an iterable, then fail gracefully if it does not work on the given object. The Python glossary:

    Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs the EAFP (Easier to Ask Forgiveness than Permission) style of programming.

    ...

    try:    _ = (e for e in my_object) except TypeError:    print my_object, 'is not iterable' 
  3. The collections module provides some abstract base classes, which allow to ask classes or instances if they provide particular functionality, for example:

    from collections.abc import Iterable  if isinstance(e, Iterable):     # e is iterable 

    However, this does not check for classes that are iterable through __getitem__.

like image 180
miku Avatar answered Sep 21 '22 17:09

miku