How do I test if an object is a sequence?

Lonnie Princehouse fnord at u.washington.edu
Mon Dec 22 16:25:55 EST 2003


> Is there a common idiom for testing if an object is a sequence?

Try iterators...

def is_sequence(thing):
  try:
    iter(thing)
    return True   
  except TypeError:
    return False

Of course, this will return True for strings, but I suspect most 
people would say that strings are sequences.  You will have
to special-case them.




More information about the Python-list mailing list