is_iterable function.

Neil Cerutti horpner at yahoo.com
Wed Jul 25 21:26:57 CEST 2007


On 2007-07-25, danmcleran at yahoo.com <danmcleran at yahoo.com> wrote:
> You can use the built-in dir() function to determine whether or not
> the __iter__ method exists:

I think you might have to check for __getitem__, also, which may
support the sequence protocol.

> def is_iterable(thing):
>     return '__iter__' in dir(thing)

So then:

def is_iterable(thing):
  return '__iter__' in dir(thing) or '__getitem__' in dir(thing)

Speaking of the iter builtin function, is there an example of the
use of the optional sentinel object somewhere I could see?

-- 
Neil Cerutti



More information about the Python-list mailing list