Skip to content Skip to sidebar Skip to footer

Python: Check A Function's Default Arguments

Given a function (e.g. create_instances) which takes many keyword arguments, how do I know which arguments have default values, and what exactly are the default values? To give fu

Solution 1:

inspect.getfullargspec(ec2.create_instance) should normally give you everything you need. Align args and defaults on the right side. For example:

deffoo(a, b=3, *c, d=5):
    m = a + b
    return m

argspec = inspect.getfullargspec(ec2.create_instance)
{**dict(zip(argspec.args[-len(argspec.defaults):], argspec.defaults)),
 **argspec.kwonlydefaults}
# => {'b': 3, 'd': 5}

As @9769953 says, if a parameter is bundled in **kwargs, it is processed by the function's code, and is thus not found in the function signature.

Post a Comment for "Python: Check A Function's Default Arguments"