spa*_*ara 0 python string concatenation default-arguments
这是我的一段代码......
def contactMaster(data="",url= str(chosenMaster)+"todolist"):
print "url: "+url
Run Code Online (Sandbox Code Playgroud)
它只打印"todolist"而不是"http://www.mysite.com/blah/1234/todolist"
为什么不工作?
定义函数时,不是在执行函数时计算默认参数.因此,如果chosenMasterPython定义时为空contactMaster,则仅打印todolist.
你需要str(chosenMaster)进入这个功能.
有关详细信息,请参阅Python教程中的默认参数值.那里的例子是:
默认值在定义范围内的函数定义点进行计算,以便进行
i = 5
def f(arg=i):
print arg
i = 6
f()
Run Code Online (Sandbox Code Playgroud)
将打印
5.