1. First introduce the object references under Python1). Python does not allow programmers to choose whether to use a value or to pass a reference. Python parameter passing is definitely a way to "pass an object reference". In fact, this approach is equivalent to a synthesis of value-passing and reference. If a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference". If a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value".
2). When people copy a list or dictionary, the reference to the object list is duplicated, and if the referenced value is changed, the original parameter is modified.
3). To simplify memory management, Python uses a reference counting mechanism for automatic garbage collection, and each object in Python has a reference count that counts how many times the object has been referenced in different places. Whenever a Python object is referenced, the corresponding reference count increases by 1, and whenever a Python object is destroyed, the corresponding reference is reduced by 1, and the Python object is actually removed from memory only if the reference count is zero.
2. del Delete the reference instead of deleting the object, the object is deleted by the automatic garbage collection mechanism
See this example:
>>> x = 1
>>> del X
>>> x
Traceback (most recent):
File "<pyshell#6>", line 1, in <module>
X
Nameerror:name ' x ' is not defined
>>> x = [' Hello ', ' World ']
>>> y = x
>>> y
[' Hello ', ' World ']
>>> x
[' Hello ', ' World ']
>>> del X
>>> x
Traceback (most recent):
File "<pyshell#12>", line 1, in <module>
X
Nameerror:name ' x ' is not defined
>>> y
[' Hello ', ' World ']
>>>
You can see that x and Y point to the same list, but when you delete x, Y is not affected. What is this for?
The reason for this is so you only delete the Name,not the list itself,in fact, there is the no-it-it-to-delete values in Pyth On (and your don ' t really need to because the Python interpreter does it by itself whenever you don't use the value anymore)
To give an example, a data (such as a list in an example) is a box, and we assign it to a variable x, which is like putting a label x on the box and then pasting y to represent the data, but using Del to delete the variable x is like tearing the label labeled X, leaving the label for Y.
Let's look at an example:
Shoplist = [' Apple ', ' mango ', ' carrot ', ' banana ']
Print (' The first item I'll buy is ', shoplist[0])
Olditem = shoplist[0]
Del Shoplist[0] #del的是引用, not object
Print (' I bought the ', Olditem)
Print (' My shopping list is now ', shoplist)
Print (Shoplist[0])
The result is:
The first item I'll buy is Apple
I bought the apple
My shopping list is now [' Mango ', ' carrot ', ' banana ']
Mango
Python object reference and del Delete reference