Created
June 29, 2012 11:31
-
-
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.
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 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