Last active
August 29, 2015 14:02
-
-
Save technige/caca2ebae2d8b5c320ad to your computer and use it in GitHub Desktop.
Python 2/3 function to detect collection objects
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_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