How to test if object is sequence, or iterable?

2006-07-22 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is sequence, or iterable?

2006-07-22 Thread 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is sequence, or iterable?

2006-07-22 Thread Tim N. van der Leeuw

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is sequence, or iterable?

2006-07-22 Thread Terry Reedy

Tim N. van der Leeuw [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 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



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is sequence, or iterable?

2006-07-22 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to test if object is sequence, or iterable?

2006-07-22 Thread Bruno Desthuilliers
Marc 'BlackJack' Rintsch a écrit :
 In [EMAIL PROTECTED], 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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to test if object is sequence, or iterable?

2006-07-22 Thread 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


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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test if object is sequence, or iterable?

2006-07-22 Thread Terry Reedy

Nick Vatamaniuc [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 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



-- 
http://mail.python.org/mailman/listinfo/python-list