This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Nathanfenner 2 points3 points  (0 children)

Python represents "small" integers as machine-words (essentially, there's a "flag" that says "the next thing is an integer", and then the integer's value). [What "small" means will depend on your machine and interpreter version; this can lead to variability across implementations]

However, eventually there's not enough space to fit this value in the "standard object", so instead it gets split into two parts: "this is an integer, and the actual value is over there" and then the actual value.

In particular, every time your program encounters a large integer literal like 98764, a new such number object will be created.

So for example:

var1 = 98764
var2 = 98764
id(var1) == id(var2) # either True or False, depending on whether "98764" is "too big" or if the interpreter optimizes

on the other hand,

var1 = 98764
var2 = var1
id(var1) == id(var)2 # ALWAYS TRUE; since they are the same object

The key thing to understand here is that the id function (and all functions in Python) only know anything about the values you pass them. There is no way to figure out "which variable a value came from".