I would like to check, whether a variable has been assigned or not. Based on the answers in this question Referring to the null object in Python, I tried it with if outputVariable==None
but this does not work and throws the error "UnboundLocalError: local variable 'outputVariable' referenced before assignment".
I have the following code:
def testMethod(assignVariable):
if assignVariable == True:
outputVariable = 5
if outputVariable==None:
outputVariable = 0
return outputVariable
returnValue = testMethod(False)
Any idea how I can check before the return statement, if outputVariable
has a value. If it does not, it should be given a value of 0
Update: Based on the accepted answer here How do I check if a variable exists?, I tried the following but it was also not successfull:
def testMethod(assignVariable):
if assignVariable == True:
outputVariable = 5
if outputVariable in locals():
pass
else:
outputVariable = 0
return outputVariable
returnValue = testMethod(False)