For this function
def eat_dog(name, should_digest=True):
print "ate dog named %s. Digested, too? %" % (name, str(should_digest))
I want to, external to the function, read its arguments and any default values attached. So for this specific example, I want to know that name has no default value (i.e. that it is a required argument) and that True is the default value for should_digest.
I’m aware of inspect.getargspec(), which does give me information about arguments and default values, but I see no connection between the two:
ArgSpec(args=['name', 'should_digest'], varargs=None, keywords=None, defaults=(True,))
From this output how can I tell that True (in the defaults tuple) is the default value for should_digest?
Additionally, I’m aware of the “ask for forgiveness” model of approaching a problem, but unfortunately output from that error won’t tell me the name of the missing argument:
>>> eat_dog()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: eat_dog() takes at least 1 argument (0 given)
To give context (why I want to do this), I’m exposing functions in a module over a JSON API. If the caller omits certain function arguments, I want to return a specific error that names the specific function argument that was omitted. If a client omits an argument, but there’s a default provided in the function signature, I want to use that default.
Python3.x
In a python3.x world, you should probably use a
Signatureobject:Python2.x (old answer)
The args/defaults can be combined as:
Here
a.args[-len(a.defaults):]are the arguments with defaults values and obviouslya.defaultsare the corresponding default values.You could even pass the output of
zipto thedictconstructor and create a mapping suitable for keyword unpacking.looking at the docs, this solution will only work on python2.6 or newer since I assume that
inspect.getargspecreturns a named tuple. Earlier versions returned a regular tuple, but it would be very easy to modify accordingly. Here’s a version which works with older (and newer) versions:Come to think of it:
would also work and may be more intuitive to some people.