In learning Python, I found that when two "names" (or "variables") are assigned to the same value, both of them point to the same memory address. For example
>>> a = 10
>>> b = 10
>>> a is b
True
My question is, when assigning b
, how does Python figure out that a 10
object already exists?
One way might be to create the new object for b
, scan pre-existing objects in memory to find a duplicate and, if found, point b
to it. This sounds expensive, and tricky for more complex objects.
2 Answers 2
It just keeps a cache of small integer instances.
>>> a = 58435
>>> b = 58435
>>> a is b
False
Precisely how small is easy to determine manually for your implementation. Mine caches −5 to 256.
-
That is disappointing - I was hoping for something fancy! By the way, it seems
float
s are cached too, e.g.a = 10245645646.3; b = 10245645646.3; a is b
yieldsTrue
.wsaleem– wsaleem2014年06月23日 20:38:41 +00:00Commented Jun 23, 2014 at 20:38 -
I think recent numbers are also cached. I am getting caching behavior for numbers well outside my caching range (-5 to 256). e.g.
b = 387420489; c = 387420489; b is c
is alsoTrue
. Interestingly,d = 9**9; d is b
givesFalse
even thoughd==b
isTrue
.wsaleem– wsaleem2014年06月23日 20:48:35 +00:00Commented Jun 23, 2014 at 20:48 -
@wsaleem But if you split into separate lines in the REPL (instead of using the semicolon) you get
False
. But then if you wrap it in a function, you getTrue
again. Interesting. May have something to do with the interpreter’s optimizations and with local vs. global variables.Vasiliy Faronov– Vasiliy Faronov2014年06月23日 21:48:01 +00:00Commented Jun 23, 2014 at 21:48 -
1Related on CodeGolf.SE: Write a program that makes 2 + 2 = 5 the tinkering with the integer cache in particular.user40980– user409802014年06月23日 22:00:29 +00:00Commented Jun 23, 2014 at 22:00
is
operator is not the same as ==
.
While ==
checks the value of object, is
checks identity of it using it's memory address.
As mentioned before ints from -5 to 256 (inclusive) are special (small) and Python will not recreate those objects. Ints from outside that range will be new objects every time so is
will return True only if they are the same exact object.
That happens also to: chars, empty strings and some other strings - that looks like Python identifiers.
(I think that bytes as well, but I'm not sure).
If you want to read more, please take a look the fallowing post: http://mrfuxi.github.io/blog/python-gotchas/
I found that when two "names" (or "variables") are assigned to the same value, both of them point to the same memory address.
-> This is not true.