How do I test if an object is a sequence?

Simon Burton simonb at webone.com.au
Mon Dec 22 21:38:59 EST 2003


On Mon, 22 Dec 2003 15:11:55 +0100, Max M wrote:

> 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.
> 
> 
> There are a few direct approaches:
> 
> 
> def is_sequence(unknown):
>      return type(unknown) = type([]) or type(unknown) = type(())
> 

    return type(unknown) in (list,tuple)

this works in python 2.2 for me. I don't use a function for it tho.

Simon Burton.









More information about the Python-list mailing list