How do I test if an object is a sequence?

Aahz aahz at
Mon Dec 22 18:30:35 CET 2003

In article <vuea4oagaq1rd6 at>,
John Roth <newsgroups at> wrote:
>"Max M" <maxm at> wrote in message
>news:3fe6fbb1$0$69983$edfadb0f at
>> 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           <*>

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