You can use func_defaults:
https://docs.python.org/2/library/inspect.html?highlight=func_defaults#types-and-members
func_defaults tuple of any default values for arguments
def some_func(a,b='myDefaultValue'):
print a, b
def wrapper(a,b):
b = some_func.func_defaults[0] if b is None else b
some_func(a,b)
print "b is 'there'"
a = "hello"
b = "there"
wrapper(a,b)
print "b is 'None'"
b = None
wrapper(a,b)
output:
b is 'there'
hello there
b is 'None'
hello myDefaultValue
EDIT: To answer your question from the comments, there isn't anything built-in to look up the arguments of the function with default values by name. However, you know that the arguments with default values have to come after the non-optional arguments. So if you know the total number of arguments you have, and how many of them have default values, you can subtract the 2 to get the starting point of the arguments with default values. Then you can zip the list of arguments (starting at the previously calculated argument index) together with the list of default argument values and create a dictionary from the list. Use the inspect module to get all of the information you need:
Like so:
>>> import inspect
>>> def some_func(a,b,c,d="dee",e="ee"):
... print a,b,c,d,e
...
>>> some_func("aaa","bbb","ccc",e="EEE")
aaa bbb ccc dee EEE
>>> some_funcspec = inspect.getargspec(some_func)
>>> some_funcspec
ArgSpec(args=['a', 'b', 'c', 'd', 'e'], varargs=None, keywords=None, defaults=('dee', 'ee'))
>>> defargsstartindex = len(some_funcspec.args) - len(some_funcspec.defaults)
>>> defargsstartindex
3
>>> namedargsdict = dict(zip([key for key in some_funcspec.args[defargsstartindex:]], list(some_funcspec.defaults)))
>>> namedargsdict
{'e': 'ee', 'd': 'dee'}
In the example above, namedargsdict is your list of arguments with default values for some_func.
Further reading:
https://docs.python.org/2/library/inspect.html#inspect.getargspec
inspect.getargspec(func) Get the names and default values of a Python
function’s arguments. A tuple of four things is returned: (args,
varargs, keywords, defaults). args is a list of the argument names (it
may contain nested lists). varargs and keywords are the names of the *
and ** arguments or None. defaults is a tuple of default argument
values or None if there are no default arguments; if this tuple has n
elements, they correspond to the last n elements listed in args.
Changed in version 2.6: Returns a named tuple ArgSpec(args, varargs,
keywords, defaults).