3

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?

César
  • 9,939
  • 6
  • 53
  • 74
goelv
  • 2,774
  • 11
  • 32
  • 40
  • 9
    There are no booleans in your code, nor are you mutating anything. – BrenBarn Aug 10 '12 at 20:30
  • There are a number of issues with this snippet. – Wug Aug 10 '12 at 20:31
  • I have multiple try-except blocks which is why I'm not returning within the try-except block. What would be the correct way to implement this boolean functionality I need? – goelv Aug 10 '12 at 20:31
  • 2
    @goelv: 1. There are no booleans in your code. None. 2. You are not mutating anything; you are assigning `success` a *completely new value`. That is not mutation. I suggest some reading on the subject. – Ed S. Aug 10 '12 at 20:32
  • I wonder where the id you are using is coming from – César Aug 10 '12 at 20:35
  • @César: written on the fly, I'd hope. – Wug Aug 10 '12 at 20:35
  • it's pretty clear you mean `success = False` and `success = True` instead of `0` and `1` – Ryan Haining Aug 10 '12 at 21:09
  • @goelv, I provided an answer to your question. I think you should update the title of the question. May I do it? – lmiguelvargasf May 10 '18 at 23:46

5 Answers5

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')
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • thanks for the help! quick question for clarification / sake of learning, does "success=false" need to be included at all? – goelv Aug 10 '12 at 20:38
  • 1
    *Yes* (though with capital F as in the example). Every variable has to be assigned to before its value is checked. – David Robinson Aug 10 '12 at 20:39
  • 1
    @goelv yes, otherwise you'll get a `NameError` for failures because you'll be trying to inspect the value of a variable that has yet to be assigned to – Daniel DiPaolo Aug 10 '12 at 20:39
  • @DanielDiPaolo: Please see http://stackoverflow.com/a/2351203/216356 for a better understanding of typing terms. – Noctis Skytower Aug 10 '12 at 20:55
1

Yes. success will be changed to 1 on success.

Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96
1

There are a few things wrong with this snippet.

  1. Firstly, you're not using a boolean type. Python's booleans are True and False.
  2. 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: or if success == True:
  3. 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.
Wug
  • 12,956
  • 4
  • 34
  • 54
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.

minhaz1
  • 733
  • 1
  • 5
  • 13
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.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228