Home > Net >  how can we explain id() built-in function output structure?
how can we explain id() built-in function output structure?

Time:10-29

a = 5
id(a)

As you know,output is;

1936111069616

I know that this is a memory address reference of "a" variable linked between variable name and object heap and stack memory on ram in decimal format but i haven't found anything about the detailed definition of this address. If we wanted to write a user-defined function for this, how would we implement? I wonder the address structure and how we know unique format. What is the meaning of length of 13 numbers?

I searched this a lot. I haven't found exact definition for 64 bit system.

CodePudding user response:

The documentation explains id as much as it can be explained:

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

That's it. It's an integer with no further semantics of its own. Anything further is something specific to a particular implementaton of Python. The reference implementation that many people think of as Python is CPython, in which case...

CPython implementation detail: This is the address of the object in memory.

... the particular integer is an address. But that's not something you can rely on or use. In any case, the implementation has to provide the value, because the implementation is in charge of managing the object over the course of its lifetime.

CodePudding user response:

There is a link to the number, but as others have mentioned:

It's an integer with no further semantics of its own

You could inspect where values are assigned by looking at the delta-memory:

x = 1
y = 2

a = 10
b = 20


a100 = 10
a101 = 11

memory_x = id(x)
memory_y = id(y)

memory_a = id(a)
memory_b = id(b)

memory_a100 = id(a100)
memory_a101 = id(a101)

print(id(x), id(y))
print(id(memory_x), id(memory_y))

mem_gap = id(y)-id(x)
print(mem_gap)

mem_gap = id(b)-id(a)
print(mem_gap)

mem_gap = id(a101)-id(a100)
print(mem_gap)

which give this:

1436097446128 1436097446160
1436179060400 1436179058480
32
320
32

so there is a consistency between the memory space of adjacent (integer) values 32, but it is not immediately intuitive.

And the memory space between the floats are larger before looking at other types. The links take one to the implementation of memory in CPython, so the answer is there or by inspection as per the above.

(ie. its not any more clear in the docs).

  • Related