I have the following code in python:
def update(request, id):
success = 0
try:
product = Mattress.objects.get(id=id)
success = 1
except Mattress.DoesNotExist:
pass
if success == 1:
return render_to_response("success.html")
else:
return render_to_response('failure.html')
Is this code a valid way to check the “success” boolean. If the code passes through the try statement, will “success” be changed to 1 or is it remaining at 0?
Answering your question:
Yes and no. Variables that are assigned a boolean value are (probably always, and definitely in this case) mutable, yes. They’re also not restricted to being assigned boolean values, as variables are not staticly typed.
But the booleans
TrueandFalsethemselves are not mutable. They are singletons that cannot be modified.Looking at your actual code:
Is not valid syntax, you want a
==there. Also, idiomatically speaking you should not use0and1for success and failure values you should useTrueandFalse. So you should refactor to something like this: