0

I found an interesting thing of using "is" to judge whether two variable are same. Here are my code and results:

a = 256
b = 256
c = 257
d = 257
print(a is b)
print(c is d)

and the results show:

True
False

Actually, when the number is bigger than 256, it turns 'False'.

Another example:

a = 'a' * 20
b = 'a' * 20
c = 'a' * 21
d = 'a' * 21
print(a is b)
print(c is d)

Results:

True
False

Similarly, when n in "'a' * n" is bigger than 20, it turns 'False'.

But when I put this into a function, it doesn't matter how big is the number:

def is_test(a):
    x = a
    y = a
    print(x is y)

is_test(257)
is_test('a' * 21)

the results are always 'True'

I ran the code on Jupyter Notebook. Anyone can explain this to me? Thanks

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149

4 Answers4

2

is checks if they are the same object(same place in memory) == checks if they are of equal value, low values are pre stored an thus point to the same memory space. for example, it would be stupid to write a new 1 and 0 every time it was needed just because it's for a new use, since those two numbers are being used so often so they just point to a prewritten datatable containing 1 and 0, among with low numbers and other often used values.

Jonas Wolff
  • 2,034
  • 1
  • 12
  • 17
0

First, is checks whether the two identifiers are references to the same object, while == checks for equality, and it calls __eq__() where applicable.

In CPython, integral numbers from -5 to 256 are "cached" at startup, and when you do a = 256, you just get back a reference to the predefined int object. For 257 and up, it's created on-the-go, so you may get two int objects with value 257.

For strings of length 20 and less, and whose value is a valid identifier*, they're also cached the same way. That's why when you define such strings twice you get the same object reference.

* For example, the value of string "a1b2c3" is a valid identifier, while ">:})_!#*^" isn't.

Source.

iBug
  • 35,554
  • 7
  • 89
  • 134
0

You can reference to Plain Integer Objects here.

"The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)"

That's why you will get the same id() for number of -5 ~ 256. As for other numbers, it would be referenced differently.

is is the way to identify is the are referencing the same object, but == checks for if the values are equal.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
Walter
  • 21
  • 4
0
a = 256
b = 256
c = a

if (a == b):
    print("True")
else:
    print("False")

if (a is b):
    print("True")
else:
    print("False")

if (a is c):
    print("True")
else:    
    print("False")

Output:

True
False
True

Description

  1. True: Output of the first if condition is True as both a and b have same value.

  2. False: Second if condition shows False because a and b are at different memory locations. Hence they refer to different objects. We can check it with id() function in python which returns the identity of an object.

  3. True: Output of the third if condition is True as both a and c are pointing to the same object.

Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34