0

I was doing a little bit of reading on how integers are represented in Python. I found this really neat article (http://www.laurentluce.com/posts/python-integer-objects-implementation/) which explains how everything set up, and how the set of "small" numbers (-5 to 256) are essentially created for free at the start of your program in a big array, so if I had

x = 5 y = 5

both x and y would be pointing to the same universal 5 object that was created at the program's start. The article gives an example that if x or y were to be set to 300 (out of range of the small numbers) then simply a new integer would be created in the next block of free space representing 300.

Does this mean that if I set both x and y to 300, they would both know to point to the same 300? It makes sense with the 5 because you can just index into the special small integer data structure offsetting bu the negative numbers, but since any numbers larger than that are created arbitrarily, is there any way for Python to know "oh yeah, I just made a 300, I'll just point y there instead of making another one".

Just curious! Thanks in advance.

5
  • 1
    "both x and y would be pointing to the same universal 5 " I think that is implementation dependent. It may apply for CPython, it does not have to apply for other Python implementations. And I doubt it is guaranteed even for CPython. In other words: do not rely on it. Commented Dec 9, 2016 at 4:38
  • You can do this yourself using is: x is y (which, of course, should only be used to compare against singletons like True, False or None; and for those cases, you generally just use if a: or if not b:). Commented Dec 9, 2016 at 4:40
  • Here's a fun trick: on the Python prompt, try >>> x = 300; y = 300; x is y. Not replace those semi-colons by newlines (i.e., use 3 lines) and do the same. Commented Dec 9, 2016 at 4:43
  • In general, even in current CPython, each int literal with a value of 300 will result in a new int object with a value of 300. There may be special circumstances in which the duplication is noticed. The same comment applies to string literals and string objects. This is implementation and version specific. Be careful with is. Beginners often manage to draw wrong conclusions. This is partly because they do not know when they have triggered 'special circumstances'. Commented Dec 9, 2016 at 4:47
  • And to make it even trickier, some Python functions will create a new object for one of the normally cached values instead of returning a new reference. This is definitely true for Python 3. Commented Dec 9, 2016 at 13:57

1 Answer 1

1

If it's done, the compiler handles it:

import dis def f(): a = 300 b = 300 dis.dis(f) print f.__code__.co_consts 

Note the argument to LOAD_CONST each time:

 4 0 LOAD_CONST 1 (300) 3 STORE_FAST 0 (a) 5 6 LOAD_CONST 1 (300) 9 STORE_FAST 1 (b) 12 LOAD_CONST 0 (None) 15 RETURN_VALUE (None, 300) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.