views:

85

answers:

3

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.

+5  A: 

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.

Philipp
Should have mentioned that I had done this as well, and got the same result.
ahhtwer
+1  A: 

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

JimB
A: 

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

starblue