This is a piece of my code...
def contactMaster(data="",url= str(chosenMaster)+"todolist"):
print "url: "+url
It prints only "todolist" instead of "http://www.mysite.com/blah/1234/todolist"
Why isn't it working??
This is a piece of my code...
def contactMaster(data="",url= str(chosenMaster)+"todolist"):
print "url: "+url
It prints only "todolist" instead of "http://www.mysite.com/blah/1234/todolist"
Why isn't it working??
Default arguments are evaluated when the function is defined not when it is executed. So if chosenMaster
is empty when Python defines contactMaster
, it will print only todolist
.
You need to move str(chosenMaster)
into the function.
See Default Argument Values in the Python Tutorial for more info. The example there is:
The default values are evaluated at the point of function definition in the defining scope, so that
i = 5
def f(arg=i):
print arg
i = 6
f()
will print
5
.
The function definition captures the value of chosenMaster
at the time the function is declared, not when the function is called.
Do this instead:
def contactMaster(data='', url=None):
if url is None:
url = str(chosenMaster) + 'todolist'
print 'url: ' + url
Because default function arguments are determined when the function is defined. Changing chosenMaster
after the fact will have no effect.