How to test if object is sequence, or iterable?

  • Thread starter Tim N. van der Leeuw
  • Start date
T

Tim N. van der Leeuw

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
 
B

Bruno Desthuilliers

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.
 
T

Tim N. van der Leeuw

Bruno said:
Tim N. van der Leeuw a écrit :

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
 
T

Terry Reedy

Tim N. van der Leeuw said:
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
 
M

Marc 'BlackJack' Rintsch

Tim N. van der Leeuw a écrit :

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
 
B

Bruno Desthuilliers

Marc 'BlackJack' Rintsch a écrit :
What about objects that just implement an apropriate `__getitem__()`
method?

Hmmm... (quick test)

Good point.

FWIW, Terry's solution might be far better.
 
N

Nick Vatamaniuc

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
 
T

Terry Reedy

Nick Vatamaniuc said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top