0

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.

2
  • 2
    Value equality should not be confused with identity. Commented Jul 19, 2010 at 13:45
  • 2
    Since you say this happens with != as will as is not, please omit current_frame = int(row[0]) and show print repr(current_frame) and print repr(row[0]) Commented Jul 19, 2010 at 13:49

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Should have mentioned that I had done this as well, and got the same result.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.