I can check for a next()
method, but is that enough? Is there an ideomatic way?
An object is iterable if it implements the iterator protocol.
You could check the presence of __iter__()
method with:
hasattr(object,'__iter__')
in Python 2.x this approach misses str objects and other built-in sequence types like unicode, xrange, buffer. It works in Python 3.
Another way is to test it with iter method :
try:
iter(object)
except TypeError:
#not iterable