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

4

12

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)?

Emmettemmey answered 1/2, 2011 at 7:28 Comment(2)
Can I make this a Wiki instead? I'm unable to find out how to do it. I found out the answer shortly after asking the question, so since I answered my own question, it's better that I make it a wiki.Emmettemmey
@Josh: Thanks. Got a much better answer from Duncan, and marked that one as the accepted answer instead.Emmettemmey
S
11

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',))
Sharma answered 1/2, 2011 at 9:36 Comment(2)
That's much better because you also get the variable names as well as their default values (if present)!Bowe
Note that this has deprecated since Python 3.0 in favour of inspect.getfullargspecBarouche
B
10

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')
Bowe answered 1/2, 2011 at 9:14 Comment(1)
Check out Duncan's answer, it's much better than this one.Bowe
E
1

The inspect.signature also provides a nice way to iterate parameters of a function https://docs.python.org/3/library/inspect.html#inspect.signature

import inspect

def f(*cake, one, two=None, three=3): pass

s = inspect.signature(f)
# <Signature (*cake, one, two=None, three=3)>
s.parameters['three'].default
3
s.parameters['cake'].kind
# <_ParameterKind.VAR_POSITIONAL: 2>
s.parameters['cake'].name
'cake'
Expenditure answered 24/2, 2020 at 2:58 Comment(0)
E
0

My bad. Of course, there's myFunction.func_defaults.

Emmettemmey answered 1/2, 2011 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.