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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They are stored in
test.func_defaults(python 2) and intest.__defaults__(python 3).As @Friedrich reminds me, Python 3 has “keyword only” arguments, and for those the defaults are stored in
function.__kwdefaults__