3

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

2 Answers 2

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.

4
  • That is disappointing - I was hoping for something fancy! By the way, it seems floats are cached too, e.g. a = 10245645646.3; b = 10245645646.3; a is b yields True.
    – wsaleem
    Commented 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 also True. Interestingly, d = 9**9; d is b gives False even though d==b is True.
    – wsaleem
    Commented 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 get True again. Interesting. May have something to do with the interpreter’s optimizations and with local vs. global variables. Commented Jun 23, 2014 at 21:48
  • 1
    Related on CodeGolf.SE: Write a program that makes 2 + 2 = 5 the tinkering with the integer cache in particular.
    – user40980
    Commented Jun 23, 2014 at 22:00
0

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.