11👍
Answering your question:
Are booleans mutable in python?
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 True
and False
themselves are not mutable. They are singletons that cannot be modified.
Looking at your actual code:
if success = 1:
Is not valid syntax, you want a ==
there. Also, idiomatically speaking you should not use 0
and 1
for success and failure values you should use True
and False
. So you should refactor to something like this:
def update(request):
success = False
try:
product = Mattress.objects.get(id=id)
success = True
except Mattress.DoesNotExist:
pass
if success:
return render_to_response("success.html")
else:
return render_to_response('failure.html')
- [Django]-How to import django models in scrapy pipelines.py file
- [Django]-Two process in one Heroku app vs two heroku apps
1👍
There are a few things wrong with this snippet.
- Firstly, you’re not using a boolean type. Python’s booleans are
True
andFalse
. - Second, you’re not comparing in your if statement. That line isn’t valid in Python 3. What you’re looking for is:
if success == 1:
orif success == True:
- You would still be able to assign a boolean value to a variable regardless of boolean immutability. I believe they are stored as a primitive type, so they’re as immutable as any other primitive type.
- [Django]-Django – ManyToMany as hidden fields?
- [Django]-Django key based session expiration
- [Django]-Django– Deactivate User account instead of deleting it
- [Django]-Django Admin Custom Widget for ForeignKey
1👍
You should consider using an actual boolean and not an integer. You can set success
to true
or false
and Python will interpret them as keywords to the values of 1 and 0. Using numbers can be a bit tricky as some languages interpret things different. In some languages 0 is false
but any value besides 0 is considered true
. However the answer to your question is yes, it will work just fine.
- [Django]-Setting global variables with Cactus App for Mac
- [Django]-Ngnix – duplicate upstream "app_server" in /etc/nginx/sites-enabled/django
- [Django]-How do you access/configure summaries/snippets in Django Haystack
- [Django]-Can this result in a deadlock in django + postgres?
- [Django]-Error in django interactive shell on pydev eclipse
1👍
Probably the question you are asking is not even related with the problem you are trying to solve. I think there is a a Pythonic way to achieve what you want by:
def update(request):
try:
product = Mattress.objects.get(id=id)
except Mattress.DoesNotExist:
template_name = 'failure.html'
else:
template_name = 'success.html'
return render_to_response(template_name)
Basically if the exception is thrown, i.e., the template you will render will be ‘failure.html’. On the other hand, if the query is performed successfully, ‘success.html’ will be rendered.
- [Django]-Can I have 2 Django sites using 2 different version of Python on 1 domain?
- [Django]-Django SESSION_COOKIE_DOMAIN
- [Django]-NoReverseMatch in django production server
- [Django]-Creating custom list filter in django