How to test if object is sequence, or iterable?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Jul 22 16:31:04 EDT 2006


Marc 'BlackJack' Rintsch a écrit :
> In <44c26ec2$0$21607$636a55ce at 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.



More information about the Python-list mailing list