Skip to content

Instantly share code, notes, and snippets.

@onelharrison
Created December 17, 2020 22:33
Show Gist options
  • Select an option

  • Save onelharrison/3a5b95638dbbeb59d7f61b642823382b to your computer and use it in GitHub Desktop.

Select an option

Save onelharrison/3a5b95638dbbeb59d7f61b642823382b to your computer and use it in GitHub Desktop.
Implementation of a safe type casting function in Python
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