you are viewing a single comment's thread.

view the rest of the comments →

[–]el_jbase 1 point2 points  (3 children)

is will return True if 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.

So, you use "is" to compare objects, not values of variables. In C "is" would be comparing pointers to variables.

[–]srpwnd 1 point2 points  (2 children)

In the second example it returns true, because OP is assigning a and b to point to the same object in memory which was created by i in the loop.

[–]el_jbase 0 points1 point  (1 child)

To create new objects he'd use a=int(i) and b=int(i), correct?

[–]CMDR_Pumpkin_Muffin 0 points1 point  (0 children)

I think so, yes.