Python memory management information - id ()

Game with id(). We started by looking at the addresses of identical attributes in non-identical objects. But now it does not matter. Before the code:

class T(object):
    pass

class N(object):
    pass

First test (in the interactive console):

n = N()
t = T()
id(n)
# prints 4298619728
id(t)
# prints 4298619792

No wonder really. n.__class__differs from t.__class__, so it seems obvious that it is impossible to be the same object. Is the __class__difference only between these objects at this time? Assuming not like:

>>> n1 = N()
>>> n2 = N()
>>> id(n1) == id(n2)
False

Python , , , n1, n2, , ( ) n1, n2? ? , , , , ( ), , .

, , T() N() - :

>>> id(N())
4298619728
>>> id(N())
4298619792
>>> id(N())
4298619728
>>> id(N())
4298619792

?

. , , shell:

>>> id(N()), id(T())
(4298619728, 4298619728)
>>> id(N()), id(T())
(4298619728, 4298619728)
>>> id(N()), id(T())
(4298619728, 4298619728)

, N() T() . , , N() id(), .

, . , - , , , - , , ?

.

+5
5

. , , , , (, ).

: id

>>> n1 = N()
>>> n2 = N()
>>> id(n1) == id(n2)
False

, Python , . , , ! , , :

>>> n1 = N()
>>> n2 = n1
>>> id(n1) == id(n2)
True

: --?

, Python . , :

  • ;
  • ( , );
  • .

, . , . ? ?

:

CPython id (!) . . builtin_id bltinmodule.c, 907.

Python, __init__ __del__:

class N:
    def __init__(self):
        print "Creating", id(self)
    def __del__(self):
        print "Destroying", id(self)

>>> id(N())
Creating 4300023352
Destroying 4300023352
4300023352

, Python , . Python , , , , , . :

>>> id(N()), id(N()), id(N())
Creating 4300023352
Destroying 4300023352
Creating 4300023352
Destroying 4300023352
Creating 4300023352
Destroying 4300023352
(4300023352, 4300023352, 4300023352)

: ""

, "", ( ). , ​​ Python ? , N?

, , N object.

, , , , , .

, Python. obmalloc.c , . .

...

? . , , Python , : 4- , , .

Python : , -. , N , , , , . , :

  • id(N())

  • Python P ( A).

  • Python P.

  • - , . , P, A.

  • id(N()).

  • Python P . A ( O), B.

  • O, , A P.

, . , id(N()),id(N()), , .

, . , , , .

: ?

. pythonrun.c, :

  • , .

  • , , .

  • , -.

  • , , , ..

, "". ( ); ( ). , .

+6

:

id(object):

"" . ( ), . id().

, , . , id , .

>>> n1 = N()
>>> n2 = N()
>>> id(n1) == id(n2)
False

"" . ; , .

CPython - . : , , - , . , id(N()), id(T()): , .

, , / .

+5

, , , . N() T() , , GC. .

+1

, .

, , ( Java ), , .

0

, , python _. , python , , . , :

print id(X())
print id(X())
print id(X())
print id(X())
print id(X())

>>> print id(X())
3078933196
>>> print id(X())
3078933004
>>> print id(X())
3078932716
>>> print id(X())
3078933196
....

python

3079140908
3079140908
3079140908
3079140908
3079140908
0

All Articles