How do I test if an object is a sequence?

John Roth newsgroups at jhrothjr.com
Mon Dec 22 12:23:36 EST 2003


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

John Roth






More information about the Python-list mailing list