2

Using Python2.7, if I try to compare the identity of two numbers, I don't get the same results for int and long.

int

>>> a = 5
>>> b = 5
>>> a is b
True

long

>>> a = 885763521873651287635187653182763581276358172635812763
>>> b = 885763521873651287635187653182763581276358172635812763
>>> a is b
False

I have a few related questions:

  • Why is the behavior different between the two?
  • Am I correct to generalize this behavior to all ints and all longs?
  • Is this CPython specific?
rahmu
  • 5,708
  • 9
  • 39
  • 57

4 Answers4

5

This isn't a difference between int and long. CPython interns small ints (from -5 to 256)

>>> a = 257
>>> b = 257
>>> a is b
False
jamylak
  • 128,818
  • 30
  • 231
  • 230
4

Why is the behavior different between the two?

When you create a new value in CPython, you essentially create an entire new object containing that value. For small integer values (from -5 to 256), CPython decides to reuse old objects you have created earlier. This is called interning and it happens for performance reasons – it is considered cheaper to keep around old objects instead of creating new ones.

Am I correct to generalize this behavior to all ints and all longs?

No. As I said, it only happens for small ints. It also happens for short strings. (Where short means less than 7 characters.) Do not rely on this. It is only there for performance reasons, and your program shouldn't depend on the interning of values.

Is this CPython specific?

Not at all. Although the specifics may vary, many other platforms do interning. Notable in this aspect is the JVM, since in Java, the == operator means the same thing as the Python is operator. People learning Java are doing stuff like

String name1 = "John";
String name2 = "John";
if (name1 == name2) {
    System.out.println("They have the same name!");
}

which of course is a bad habit, because if the names were longer, they would be in different objects and the comparison would be false, just like in Python if you would use the is operator.

kqr
  • 14,791
  • 3
  • 41
  • 72
2

Why is the behavior different between the two?

This is because only some ints are really interned. It cannot be done on really all values.

Am I correct to generalize this behavior to all ints and all longs?

In the case of int, no. Some are interned, some not.

AFAICT, for longs, you may be right, but I am not sure. I tested it with two 4Ls, and they were not identical.

Is this CPython specific?

Yes. It is not specified in the language specs, so this behaviour may (and will) differ on other implementations.

glglgl
  • 89,107
  • 13
  • 149
  • 217
-2

Try this way then you will be able to compare the data

a = 885763521873651287635187653182763581276358172635812763
b = 885763521873651287635187653182763581276358172635812763
if a == b:
 print 'both are same'

Then you will get result

both are same
Amit Vikram
  • 394
  • 2
  • 15