Skip to content

Instantly share code, notes, and snippets.

@gjbagrowski
Created April 22, 2015 10:31
Show Gist options
  • Save gjbagrowski/eddbdd0afd1c475d0975 to your computer and use it in GitHub Desktop.
Save gjbagrowski/eddbdd0afd1c475d0975 to your computer and use it in GitHub Desktop.
def is_enumerable(collection):
"""Checks if the variable is not a basestring instance and can be
enumerated.
"""
try:
# strings can be iterated - that's not what we want
if isinstance(collection, basestring):
return False
# avoid opening a generator
if isinstance(collection, types.GeneratorType):
return True
for a in collection:
break
return True
except TypeError:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment