2014-07-01 23 views
1

参数是有可能追加到一个默认的函数参数或从外部获取函数参数的值?追加到默认的功能

例如:

def foo(bar, foo="baz"): 
    print(bar, foo) 

foo("hello", "world") # print "hello world" 
foo("hello", foo="world") # print "hello world" as well 
foo("hello") # print "hello baz" 
foo("hello", foo<append it>"bla") # how? want to print "hello bazbla" 
print("default value of foo's foo parameter:", [magic here]) 

我不认为这是一个很好的做法,但我很好奇。

+0

有什么错调用'FOO( “你好”, “bazbla”)'?你想修改函数本身吗? – bereal

+0

[获取函数参数的默认值?]的可能重复(http://stackoverflow.com/questions/12627118/get-a-function-arguments-default-value) –

回答

1
>>> foo.__defaults__ 
('baz',) 

__defaults__属性 是documented here


所以,改变使用默认值foo的参数,你可以使用字符串格式化:

foo("hello", foo="{}bla".format(foo.__defaults__[0]))