Created
August 25, 2014 19:47
-
-
Save zhester/1d4044feaa563a666064 to your computer and use it in GitHub Desktop.
Determine if any object is a sequence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#============================================================================= | |
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