Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a function argument's default value?

Tags:

python

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.

like image 430
skyler Avatar asked Sep 27 '12 17:09

skyler


People also ask

Can a function argument have default value?

Once a default value is used for an argument in the function definition, all subsequent arguments to it must have a default value as well. It can also be stated that the default arguments are assigned from right to left.

How do you define a function with default value?

Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.

Can functions have default values?

We can also use a function as a default value. Functions are first-class citizens in JavaScript and can be treated like any other variable. We can set a default value by evaluating a function and using the value returned.

How can we pass arguments to functions by default?

In addition to passing arguments to functions via a function call, you can also set default argument values in Python functions. These default values are assigned to function arguments if you do not explicitly pass a parameter value to the given argument. Parameters are the values actually passed to function arguments.

What is the default value of a function parameter without argument?

If we call the function without an argument, it uses the default value ("Norway"): A parameter with a default value, is often known as an " optional parameter ". From the example above, country is an optional parameter and "Norway" is the default value.

What are default values of default arguments in Python?

Here a.args [-len (a.defaults):] are the arguments with defaults values and obviously a.defaults are the corresponding default values. You could even pass the output of zip to the dict constructor and create a mapping suitable for keyword unpacking.

How do I set default parameter values for JavaScript functions?

Learn how to set default parameter values for JavaScript functions. Default Parameters If a function in JavaScript is called with missing arguments(less than declared), the missing values are set to undefined. Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter: Example function myFunction(x, y) {

What happens if a function has no arguments in JavaScript?

If a function in JavaScript is called with missing arguments(less than declared), the missing values are set to undefined. Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter: Example function myFunction(x, y) {


3 Answers

Python3.x

In a python3.x world, you should probably use a Signature object:

import inspect  def get_default_args(func):     signature = inspect.signature(func)     return {         k: v.default         for k, v in signature.parameters.items()         if v.default is not inspect.Parameter.empty     } 

Python2.x (old answer)

The args/defaults can be combined as:

import inspect a = inspect.getargspec(eat_dog) zip(a.args[-len(a.defaults):],a.defaults) 

Here a.args[-len(a.defaults):] are the arguments with defaults values and obviously a.defaults are the corresponding default values.

You could even pass the output of zip to the dict constructor 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.getargspec returns 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:

import inspect def get_default_args(func):     """     returns a dictionary of arg_name:default_values for the input function     """     args, varargs, keywords, defaults = inspect.getargspec(func)     return dict(zip(args[-len(defaults):], defaults)) 

Come to think of it:

    return dict(zip(reversed(args), reversed(defaults))) 

would also work and may be more intuitive to some people.


like image 84
mgilson Avatar answered Sep 29 '22 15:09

mgilson


You can use inspect module with its getargspec function:

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.

See mgilson's answer for exact code on how to retrieve argument names and their default values.

like image 44
Tadeck Avatar answered Sep 29 '22 15:09

Tadeck


Depending on exactly what you need, you might not need the inspect module since you can check the __defaults__ attribute of the function:

>>> eat_dog.__defaults__
(True,)
>>> eat_dog.__code__.co_argcount
2
>>> eat_dog.__code__.co_varnames
('name', 'should_digest')
>>> 
>>> eat_dog.__kwdefaults__
>>> eat_dog.__code__.co_kwonlyargcount
0 
like image 35
Chris_Rands Avatar answered Sep 29 '22 14:09

Chris_Rands