Apr-30-2022, 07:55 PM
(This post was last modified: Apr-30-2022, 07:55 PM by deanhystad.)
Why would this ever return false?
a =500 a is 500"a" references the int object 500. This may not be the only 500 int object, but it is the same one created by "a = 500".
a = 500 print(id(a), id(500))
Output:2003347601776 2003347601776
I think you were trying to test this:a = 500 print("500 is not always 500", a is int(str(a))) b = 255 print("255 is always 255", b is int(str(b)))
Output:500 is not always 500 False
255 is always 255 True
CPython creates int objects for 0 through 256. These are used whenever an int object is needed in this range instead of creating new int objects. But this is an implementation detail and should not be used in your programs. Limit using "is" to testing if two variables reference the same object. Use "==" to test if two objects have the same value.