Why not do the same?

Why the following code changes both variables:

>>> a = [] >>> b = a >>> a.append(9) >>> a [9] >>> b [9] >>> 

But does the del operator not achieve the same effect?

 >>> a = [] >>> b = a >>> del(a) >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> b [] >>> 
+1
python append del
Apr 30 '14 at 0:12
source share
4 answers

When you do:

 a = b 

What you do is assign label b to the same object referenced by label a .

When you do:

 a.append(9) 

You add 9 to the list object that both a and b point to. This is the same object, so they show the same result.

When you do:

 del a 

You are deleting a link to an object, not an object. If this is the only link, then the object will be garbage collected. But in your case there is another link - b - so the object continues to exist.

+2
Apr 30 '14 at 0:17
source share

Instead of β€œvariables,” think in terms of names and objects.

 >>> a = [] 

This creates an empty list object and associates it with the name a .

 >>> b = a 

It just says that b now the new name for the object named a . We have

 >>> a is b True 

del a means we forget the name a : it is no longer attached to the object.

 >>> del a >>> a Traceback (most recent call last): File "<ipython-input-8-60b725f10c9c>", line 1, in <module> a NameError: name 'a' is not defined 

But you no longer call this list object a , only b does not affect the object in any way. Objects don't care, or even know what names you gave them. [One exception is that if an object no longer has references, it may or may not, it does not matter promises to collect garbage.]

+1
Apr 30 '14 at 0:17
source share

The append method works against the actual object, and del works against the reference ie the variable name.

0
Apr 30 '14 at 0:17
source share

(I already answered the question in to your other question , so I will also use it here, with minor changes :)

del does not delete objects; in fact, in Python, even the / VM interpreter cannot delete an object from memory, because Python is a garbage-collected language (e.g. Java, C #, Ruby, Haskell, etc.).

Instead, what del does when calling a variable (as opposed to a dictionary key or list item) is like this:

 del a 

lies in the fact that it only deletes a local (or global) variable, and not what it points to (each variable in Python contains a pointer / link to its contents, not the content itself). In fact, since locals and globals are stored as a dictionary under the hood (see locals() and globals() ), del a equivalent to:

 del locals()['a'] 

(or del globals()['a'] as applied to the global.)

so if you have:

 a = [] b = a 

you create a list by storing a link to it in a , and then copy that link to b without copying / touching the list object itself. Therefore, these two calls affect the same object:

 >>> a.append(1) >>> b.append(2) >>> a [1, 2] >>> b [1, 2] >>> a is b # would be False for 2 identical but different list objects True >>> id(a) == id(b) True 

( id returns the memory address of the object)

while the removal of b nothing to do with the fact that b indicates:

 >>> a = [] >>> b = a >>> del b # a is still untouched and points to a list >>> b NameError: name 'b' is not defined >>> a [] 
0
Apr 30 '14 at 10:40
source share



All Articles