Removing a Python Object

a = [1,2,3,4,5] b = a[1] print id(a[1],b) # out put shows same id.hence both represent same object. del a[1] # deleting a[1],both a[1],b have same id,hence both are aliases print a # output: [1,3,4,5] print b # output: 2 

Both b, a [1] have the same identifier, but deleting one does not affect the other. The Python reference indicates that 'del' on a subscription deletes the actual object,not the name object binding . Result: [1,3,4,5] confirms this statement. But how is it possible that "b" remains unaffected when both a[0] and b have the same identifier.

Edit: The 'del' on a subscription deletes the actual object,not the name object binding is incorrect. The converse is true. "del" actually removes the name, binds objects. In the case of "del" when subscribing (for example, del a [1]) removes object 2 from the list object, and also removes the current binding of a[1] to 2 and makes a[1] instead of 3 . Subsequent indexes follow the pattern.

+4
source share
4 answers

del does not delete objects, removes links .

There is an object that is an integer value of 2 . This one object was mentioned in two places; a[1] and b .

You deleted a[1] so that the link doesn’t disappear. But this does not affect object 2 , only the link that was in a[1] . Thus, the link accessible via name b still reaches object 2 simply.

Even if you del all links that do not affect the object. Python is a garbage collection, so it is responsible for notifying when an object is no longer referenced anywhere, so that it can return the memory occupied by the object. This will happen some time after the object is no longer available. 1


1 CPython uses reference counting to implement garbage collection 2, which allows us to say that objects will usually be fixed as soon as their last link dies, but that the implementation detail is not part of the language specification. You do not need to understand exactly how Python collects its garbage and does not have to write programs that depend on it; other Python implementations, such as Jython, PyPy, and IronPython, do not implement garbage collection this way.

2 Plus an additional garbage collection mechanism for detecting circular garbage that cannot be dealt with by reference counting.

+7
source

del simply decreases the reference count for this object. Thus, after b = a[1] object at a[1] has 2 (valid) references. After removing a [1], it disappeared from list and now has only 1 link, as b still indicates. Actual deletion does not occur until those. count is 0, and then only through the GC loop.

+6
source

There are several issues here. First, calling del on a list item removes the item from the list, which frees the reference count on the object, but it does not free it, since the variable b still refers to it. You can never free what you have a link to.

The second problem that should be noted here is that integers close to zero are actually combined and are never freed. Usually you do not need to worry about this.

+2
source

They have the same id because Python reuses id for small integers, even if you delete them ... This is mentioned in the docs :

The current implementation saves an array of integer objects for all integers from -5 to 256, when you create an int in this range, you actually just return a reference to an existing object.

We can see this behavior:

 >>> c = 256 >>> id(c) 140700180101352 >>> del c >>> d = 256 >>> id(d) 140700180101352 # same as id(c) was >>> e = 257 >>> id(e) 140700180460152 >>> del e >>> f = 257 >>> id(f) 140700180460128 # different to id(e) ! 
0
source

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


All Articles