is_iterable function.
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Thu Jul 26 13:15:30 EDT 2007
On Thu, 26 Jul 2007 15:02:39 +0000, Neil Cerutti wrote:
> Based on the discussions in this thread (thanks all for your
> thoughts), I'm settling for:
>
> def is_iterable(obj):
> try:
> iter(obj).next()
> return True
> except TypeError:
> return False
> except KeyError:
> return False
>
> The call to iter will fail for objects that don't support the
> iterator protocol, and the call to next will fail for a
> (hopefully large) subset of the objects that don't support the
> sequence protocol.
And the `next()` consumes an element if `obj` is not "re-iterable".
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list