8

How can I programmatically access the default argument values of a method in Python? For example, in the following

def test(arg1='Foo'):
    pass

how can I access the string 'Foo' inside test?

7
  • 1
    Can you provide an example demonstrating why you might want to do that? Commented Jan 10, 2012 at 16:17
  • Do you mean NOT just typing arg1? Commented Jan 10, 2012 at 16:18
  • If you don't provide arg1 when calling test, then arg1 will default to 'Foo' Commented Jan 10, 2012 at 16:21
  • @orangeoctopus: Well arg1 can be overwritten, right? Commented Jan 10, 2012 at 16:21
  • "Well arg1 can be overwritten, right?"? What do you mean by that. You can't change the default. But you can provide a argument value. What do you mean "overwritten"? Commented Jan 10, 2012 at 16:36

4 Answers 4

17

They are stored in test.func_defaults (python 2) and in test.__defaults__ (python 3).

As @Friedrich reminds me, Python 3 has "keyword only" arguments, and for those the defaults are stored in function.__kwdefaults__

Sign up to request clarification or add additional context in comments.

1 Comment

See also __kwdefaults__.
5

Consider:

def test(arg1='Foo'):
    pass

In [48]: test.func_defaults
Out[48]: ('Foo',)

.func_defaults gives you the default values, as a sequence, in order that the arguments appear in your code.

Apparently, func_defaults may have been removed in python 3.

1 Comment

I think func_defaults only works on Python 2.x. __defaults__ seems to work on Python 2.7 and 3.2.
2

Ricardo Cárdenes is on the right track. Actually getting to the function test inside test is going to be a lot more tricky. The inspect module will get you further, but it is going to be ugly: Python code to get current function into a variable?

As it turns out, you can refer to test inside the function:

def test(arg1='foo'):
    print test.__defaults__[0]

Will print out foo. But refering to test will only work, as long as test is actually defined:

>>> test()
foo
>>> other = test
>>> other()
foo
>>> del test
>>> other()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test
NameError: global name 'test' is not defined

So, if you intend on passing this function around, you might really have to go the inspect route :(

2 Comments

I was under that impression as well, turns out that test is in test's local scope as pointed out in Ricardo's comment on my answer.
If we do def test2(): print locals(), '\n\n', globals(), we see that test2 is in globals, and there is nothing in locals.
0

This isn't very elegant (at all), but it does what you want:

def test(arg1='Foo'):
    print(test.__defaults__)

test(arg1='Bar')

Works with Python 3.x too.

2 Comments

Why globals()? test is local in its own scope, no need for that.
@RicardoCárdenes, you're right. I did not know that, thank you. Fixing it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.