Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the default values of a particular function's argument in another function in Python?

Let's suppose we have a function like this:

def myFunction(arg1='a default value'):
  pass

We can use introspection to find out the names of the arguments that myFunction() takes using myFunction.func_code.co_varnames, but how to find out the default value of arg1 (which is 'a default value' in the above example)?

like image 316
Srikanth Avatar asked Feb 01 '11 07:02

Srikanth


People also ask

How will you specify a default value for an argument in the function definition in Python?

Python has a different way of representing syntax and default values for function arguments. 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.

How do you declare a default argument for the arguments of function?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn't provide a value for the argument with a default value. In case any value is passed the default value is overridden.

Can you set default values to Python arguments?

In addition to passing arguments to functions via a function call, you can also set default argument values in Python functions.

What is default arguments in functions give example?

The default value is given in the form of variable initialization. Example : void defaultvalue(int n1 = 10, n2 = 100); 3. The default arguments facilitate the function call statement with partial or no arguments.


2 Answers

As an alternative to rooting around in the attributes of the function you can use the inspect module for a slightly friendlier interface:

For Python 3.x interpreters:

import inspect
spec = inspect.getfullargspec(myFunction)

Then spec is a FullArgSpec object with attributes such as args and defaults:

FullArgSpec(args=['arg1'], varargs=None, varkw=None, defaults=('a default value',), kwonlyargs=[], kwonlydefaults=None, annotations={})

Some of these attributes are not available on Python 2 so if you have to use an old version inspect.getargspec(myFunction) will give you a similar value without the Python 3 features (getargspec also works on Python 3 but has been deprecated since Python 3.0 so don't use it):

import inspect
spec = inspect.getargspec(myFunction)

Then spec is an ArgSpec object with attributes such as args and defaults:

ArgSpec(args=['arg1'], varargs=None, keywords=None, defaults=('a default value',))
like image 144
Duncan Avatar answered Oct 01 '22 04:10

Duncan


If you define a function f like this:

>>> def f(a=1, b=True, c="foo"):
...     pass
...

in Python 2, you can use:

>>> f.func_defaults
(1, True, 'foo')
>>> help(f)
Help on function f in module __main__:
f(a=1, b=True, c='foo')

whereas in Python 3, it's:

>>> f.__defaults__
(1, True, 'foo')
>>> help(f)
Help on function f in module __main__:
f(a=1, b=True, c='foo')
like image 43
Tim Pietzcker Avatar answered Oct 01 '22 03:10

Tim Pietzcker