Tutorial :Really awkward (seemingly simple) bug with python integer comparisons


I have the following piece of code which is not working the way I expect it to at all...

current_frame = 15 # just for showcasing purposes  g_ch = 7    if (current_frame != int(row[0])) and (int(row[1]) != g_ch):                  current_frame = int(row[0])                  print "curious================================="                  print current_frame                  print row                  print current_frame, " != ", int(row[0]), ", ", current_frame != int(row[0])                  print "========================================"  

which prints for any specific case:

curious=================================     15     ['15', '1', 'more data'] 15 != 15 , False    ========================================  

This should obviously never even enter the if statement, as the equality is showing false. Why is this happening?

edit: I have also tried this with != instead of 'is not', and gotten the same results.


Value comparisons are done with the != operator, not with is not, which compares object identity.

Apart from that, I think it's an indentation problem.


In short, you need to use == and !=, and not is. is compares object identity, not equality.


You assign current_frame = int(row[0]) inside the if, which changes the value of the boolean expression.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »