Created
December 17, 2020 22:43
-
-
Save onelharrison/f3dc388ec00d52efb378191f6d5def6f to your computer and use it in GitHub Desktop.
Implementation of a safe get function in Python
This file contains hidden or 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 safe_get(collection, key, default=None): | |
"""Get values from a collection without raising errors""" | |
try: | |
return collection.get(key, default) | |
except TypeError: | |
pass | |
try: | |
return collection[key] | |
except (IndexError, TypeError): | |
pass | |
return default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment