473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to test if object is sequence, or iterable?

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
7 52973
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

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

"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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Leif K-Brooks | last post by:
Is there a word for an iterable object which isn't also an iterator, and therefor can be iterated over multiple times without being exhausted? "Sequence" is close, but a non-iterator iterable could...
18
by: alex | last post by:
Hi there, how can I check if a variable is a structure (i.e. a list)? For my special problem the variable is either a character string OR a list of character strings line So how can I test...
1
by: Daniel Poetzinger | last post by:
Hello List Sorry if this question is too simple or unreleated, but I didn't found an answer yet: How is it possible to handle an element: <test>1 2 3 4</test> as sequence of atomic types...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
5
by: tom | last post by:
Hi! My code is And it gives following error: How can sortedList variable turn into NoneType? I just don't get it...
0
by: Mikelowe | last post by:
Would like to know is there an automated way to copy the maxvalue of sequence number from db2 production to db2 test. When we copy data from our production that uses a sequence number value as its...
5
by: Anan18 | last post by:
Hello sir, I'm supposed to Implement and Test the sequence Class Using a Fixed-Sized Array (Chapter 3), from Data Structures & Other objects using c++. The header file is provided, and so is a test...
11
by: Erich | last post by:
Hello all, Today I found myself once again defining two functions that I use all the time: nsplit and iterable. These little helper functions of mine get used all the time when I work. Im sick...
3
by: william hoskins | last post by:
Hey everyone, I am fairly new to python, and I was working on a google app engine project for a class, and this error came up. I wasn't sure why, because right above it I have the same object type...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.