I saw two questions about Python:
- When to use == and when to use is?
- Why does Python not cache integer values outside the range [-5, 256]?
and I found that
a = 123.
b = 123.
print(a is b)
True
a = 257
b = 257
print(id(a) == id(b))
True
My questions:
-
The result of the first code (the code of
123.
) isTrue
, does that mean Python cachesfloat
? -
How is the result of the second code (the code of
257
)True
despite the range is still[-5,256]
? -
Why are the results different from the results in those questions (if that is just because those questions are too old and Python was changed, but that can not be applied on the second code because the range was not changed in GitHub)?
17