Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created June 29, 2012 11:31
Show Gist options
  • Save Midnighter/3017475 to your computer and use it in GitHub Desktop.
Save Midnighter/3017475 to your computer and use it in GitHub Desktop.
Often there is a situation when default input arguments need to be converted to a specific type or have a default value of a different type. It's simple to do but can look messy, so I started using this little function.
def convert_argument(arg, new_type, default=None):
"""
Convert an argument to a new type unless it is `None`.
"""
return default if arg is None else new_type(arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment