Is there a better way than the following example to detect if an optional argument has been provided? Even if None
is provided, we want to return that instead of raising the exception.
Expected results:
>>> get_value('key-exists-not') # default not provided
KeyError
>>> get_value('key-exists-not', None) # should support arg
None
>>> get_value('key-exists-not', default=None) # as well as kwarg
None
>>> get_value('key-exists-not', default='')
''
Is this the best/recommended way to do it?
def get_value(key, default='somerubbishthatwillneverbeused'):
try:
return _data[key]
except KeyError:
if default != 'somerubbishthatwillneverbeused':
return default
raise