Skip to content

Instantly share code, notes, and snippets.

@technige
Last active August 29, 2015 14:02
Show Gist options
  • Save technige/caca2ebae2d8b5c320ad to your computer and use it in GitHub Desktop.
Save technige/caca2ebae2d8b5c320ad to your computer and use it in GitHub Desktop.
Python 2/3 function to detect collection objects
def is_collection(obj):
""" Returns true for any iterable which is not a string or byte sequence.
"""
try:
if isinstance(obj, unicode):
return False
except NameError:
pass
if isinstance(obj, bytes):
return False
try:
iter(obj)
except TypeError:
return False
try:
hasattr(None, obj)
except TypeError:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment