python - Default argument alternative to None? -
i writing function little this:
def parse(string, default=none): try: ... # body of function here except parseerror: if default not none: return default raise
but run problems if want function return none
.
if len(sys.argv) > 4: if flag.ignore_invalid: result = parse(sys.argv[4], none) # still raises error else: result = parse(sys.argv[4])
what have tried fix is:
_default = object() def parse(string, default=_default): ... if default not _default: ...
but seems i'm overthinking it, , seems there's easier way it.
the any
, all
functions (allowing none
default), don't know how.
unless you're needing ignore parseerror
, let parse
throw of time, , catch in places need to:
try: parse(sys.argv[4]) except parseerror: if not flag.ignore_invalid: raise
though if must have default value, _default
solution ok.
Comments
Post a Comment