How do I test if an object is a sequence?

Aahz aahz at pythoncraft.com
Mon Dec 22 12:30:35 EST 2003


In article <vuea4oagaq1rd6 at news.supernews.com>,
John Roth <newsgroups at jhrothjr.com> wrote:
>"Max M" <maxm at mxm.dk> wrote in message
>news:3fe6fbb1$0$69983$edfadb0f at dread12.news.tele.dk...
>>
>> Is there a common idiom for testing if an object is a sequence?
>>
>> Both list, tuple and non-standard objects etc. I have Googled, but
>> didn't find a usable answer.
>
>The best way I know of is to check for the existance of either __iter__
>or __getitem__. This gets you roughly the same results as either the
>for statement or the iter() built-in function.

Not quite.  Try using ``for`` on a dict in Python 2.1 or earlier.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.




More information about the Python-list mailing list