Search code examples
pythoncomparisontuplespacman

comparing tuples in python


I have the following piece of code:

while current is not problem.getStartState():

        print "Current: ", current, "Start: ", problem.getStartState()

now for some reason the comparison is not working well, you can see in the following output:

Current:  (3, 5, 0, 0, 0, 0) Start:  (4, 5, 0, 0, 0, 0)
Current:  (4, 5, 0, 0, 0, 0) Start:  (4, 5, 0, 0, 0, 0)

you can see that even though current is the same as getStartState() it enters the while. furthermore - when it used to be a 2 fields tuple (x,y) it worked fine.

What am I doing wrong ? Thanks


Solution

  • is tests for identity, not equality. You want current != problem.getStartState()

    There is an idiom is (not) None which works because None is guaranteed to be a singleton. Don't use it for other types unless you really mean it!