My Google-fu has failed me.
In Python, are the following two tests for equality equivalent?
n = 5 # Test one. if n == 5: print 'Yay!' # Test two. if n is 5: print 'Yay!'
Does this hold true for objects where you would be comparing instances (a list say)?
Okay, so this kind of answers my question:
L = [] L.append(1) if L == [1]: print 'Yay!' # Holds true, but... if L is [1]: print 'Yay!' # Doesn't.
So == tests value where is tests to see if they are the same object?
iswill returnTrueif two variables point to the same object (in memory),==if the objects referred to by the variables are equal.In your case, the second test only works because Python caches small integer objects, which is an implementation detail. For larger integers, this does not work:
The same holds true for string literals:
Please see this question as well.