423,850 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

How to test if object is sequence, or iterable?

P: n/a
Hi,

I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().

Does something like that exist? (Something which, in case of iterable,
doesn't consume the first element of the iterable)

Regards,

--Tim

Jul 22 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Tim N. van der Leeuw a écrit :
Hi,

I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().

Does something like that exist? (Something which, in case of iterable,
doesn't consume the first element of the iterable)
isiterable = lambda obj: isinstance(obj, basestring) \
or getattr(obj, '__iter__', False)
Should cover most cases.
Jul 22 '06 #2

P: n/a

Bruno Desthuilliers wrote:
Tim N. van der Leeuw a écrit :
Hi,

I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().

Does something like that exist? (Something which, in case of iterable,
doesn't consume the first element of the iterable)

isiterable = lambda obj: isinstance(obj, basestring) \
or getattr(obj, '__iter__', False)
Should cover most cases.
Yes, that seems to cover all cases I can think of, indeed. Funny
though, that string objects do not have an '__iter__' method, but are
still iterable... But it will make most of my use-cases easier: Often I
want to iterate over something, if it's an iterable, except when it's a
string.
Thanks,

--Tim

Jul 22 '06 #3

P: n/a

"Tim N. van der Leeuw" <ti*************@nl.unisys.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,

I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().
How about
try: it = iter(possible_iterable)
except TypeError: bail()

Terry Jan Reedy

Jul 22 '06 #4

P: n/a
In <44***********************@news.free.fr>, Bruno Desthuilliers wrote:
Tim N. van der Leeuw a écrit :
>Hi,

I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().

Does something like that exist? (Something which, in case of iterable,
doesn't consume the first element of the iterable)

isiterable = lambda obj: isinstance(obj, basestring) \
or getattr(obj, '__iter__', False)
Should cover most cases.
What about objects that just implement an apropriate `__getitem__()`
method?

Ciao,
Marc 'BlackJack' Rintsch
Jul 22 '06 #5

P: n/a
Marc 'BlackJack' Rintsch a écrit :
In <44***********************@news.free.fr>, Bruno Desthuilliers wrote:

>>Tim N. van der Leeuw a écrit :
>>>Hi,

I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().

Does something like that exist? (Something which, in case of iterable,
doesn't consume the first element of the iterable)

isiterable = lambda obj: isinstance(obj, basestring) \
or getattr(obj, '__iter__', False)
Should cover most cases.


What about objects that just implement an apropriate `__getitem__()`
method?
Hmmm... (quick test)

Good point.

FWIW, Terry's solution might be far better.
Jul 22 '06 #6

P: n/a
Tim,

An object is iterable if it implements the iterator protocol. A good
enough check to see if it does is to check for the presense of the
__iter__() method. The way to do it is:
hasattr(object,'__iter__')

You are correct in the fact that you check if an object is iterable
rather than using isinstance to check if it is of a partucular type.
You are doing things 'the pythonic way' ;)

Nick Vatamaniuc
Tim N. van der Leeuw wrote:
Hi,

I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().

Does something like that exist? (Something which, in case of iterable,
doesn't consume the first element of the iterable)

Regards,

--Tim
Jul 22 '06 #7

P: n/a

"Nick Vatamaniuc" <va******@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
Tim,

An object is iterable if it implements the iterator protocol
There are presently two iterator protocols. The old one will be likely be
dropped in 3.0 (currently being discussed).
>. A good
enough check to see if it does is to check for the presense of the
__iter__() method. The way to do it is:
hasattr(object,'__iter__')
Sorry, this check for the newer and nicer protocol but not the older one.
>>hasattr('abc', '__iter__')
False

This may change in 2.6. The defacto *version-independent* way to determine
iterability is to call iter(ob). If it returns an iterator, you can
iterate; if it raises TypeError, you cannot. Any it should be patched as
necessary by the developers to continue doing the right thing in future
versions.

Terry Jan Reedy

Jul 23 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.