Skip to content

Instantly share code, notes, and snippets.

@zhester
Created August 25, 2014 19:47
Show Gist options
  • Save zhester/1d4044feaa563a666064 to your computer and use it in GitHub Desktop.
Save zhester/1d4044feaa563a666064 to your computer and use it in GitHub Desktop.
Determine if any object is a sequence.
#=============================================================================
def is_sequence( subject ):
"""
Determines if any object/variable/whatever is a sequence.
"""
# assume the subject is not a sequence
result = False
# attempt to extract the iterable representation
try:
iterator = iter( subject )
# the subject can not be iterated
except TypeError:
result = False
# the subject can be iterated
else:
# for this test, we don't count strings as sequences
if isinstance( subject, basestring ) == True:
result = False
# safe to assume the subject is a sequence
else:
result = True
# return the result of the test
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment