Created
December 17, 2020 22:33
-
-
Save onelharrison/3a5b95638dbbeb59d7f61b642823382b to your computer and use it in GitHub Desktop.
Implementation of a safe type casting 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_cast(value, astype, default = None): | |
| """Convert one type to another without raising errors""" | |
| try: | |
| return astype(value) | |
| except (TypeError, ValueError): | |
| pass | |
| return default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment