Do python objects do in memory at runtime?

If the same object is called multiple times in python, will the memory location always be the same when printing "self"?

+4
source share
3 answers

AFAIK, Cpython's memory addresses are, by design, static. The memory address of the object can be seen using id() . The name of the function is a signal that it does not change ...

See, however, the comment below, where other SO users pointed out that id() is a memory address - this is a detailed implementation of Cpython.

NTN!

+4
source

Note that, in a very high-level language, Python does not define "memory addresses" for objects. However, objects have an "identifier" that can be requested using the id() built-in function . This certificate is guaranteed not to change during the life of the object:

Return the "identifier" of the object. This is an integer (or a long integer) that is guaranteed to be unique and constant for this object throughout its life. Two objects with non-overlapping lifetimes can have the same id () value.

In CPython, this identity is the main memory address, but it is just an implementation detail; it may be different in other implementations and may even change in future versions of CPython (although this is unlikely).

+4
source

As mac noted, memory addresses in Cpython are - by design - static

But even in Cpython you cannot pass this if you use some c-extensions. Some of them can move objects and manually manage garbage collectors.

And if you use other Python implementations, like PyPy , you certainly cannot guarantee that the memory location of the objects will always be the same, and most likely the movement will move.

+1
source

Source: https://habr.com/ru/post/1414842/


All Articles