The usage of del in Python __python

Source: Internet
Author: User

the usage of del in Python a

>>> a = [1, "Two", 3, "four"]
>>> del a[0]         #删除列表a中, elements of subscript 0
>>> a
[' two ', 3, ' F Our ']
>>> a.append ("five")
>>> a.append (6)
>>> a
[' two ', 3, ' four ', ' Five ', 6]
>>> del A[2:4]        #删除a从下标为2到4的元素, with head not including tail
>>> a
[' two ', 3, 6]
>> > del a             #删除列表a
>>> a
traceback (most recent call last):
  File "<stdin>", Line 1, in & lt;module>
nameerror:name ' A ' is not defined

Usage two turn from Seventh city

Let's start with the following: Python object references
1.1 List content: Python does not allow programmers to choose whether to pass a value or a reference. The Python parameter pass is definitely a way of "passing object reference". In fact, this approach is equivalent to a combination of value-passing and reference. If a function receives a reference to a mutable object (such as a dictionary or 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-it is the equivalent of passing the object by "value".
1.2 When people copy a list or dictionary, they copy the reference to the list of objects, and if you change the value of the reference, the original argument is modified.
1.3 To simplify memory management, Python implements automatic garbage collection by referencing the counting mechanism, and each object in Python has a reference count that counts how many times the object is referenced in different locations. Each time a Python object is referenced, the corresponding reference count is incremented by 1, and whenever the 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.

del Deletes a reference instead of deleting the object, which is deleted by the automatic garbage collection mechanism
Look at this example:

>>> x = 1 >>> del x >>> x traceback (most recent call last): File " ;p yshell#6> ", line 1, in <module> x nameerror:name ' x ' isn't defined >>> x = [' Hello ', ' World '] >> ;> y = x >>> y [' Hello ', ' world '] >>> x [' Hello ', ' World '] >>> del x >>> x TRACEBAC K (most recent call last): File "<pyshell#12>", line 1, in <module> x nameerror:name ' x ' are not defined >& Gt;> y [' Hello ', ' world '] >>> 

The

can see X and Y pointing to the same list, but after removing x, Y is not affected. This is why.
The reason for the ' is ' to delete the Name,not ' list itself,in fact, there is no way to delete values in Python (and don ' t really need to because the Python interpreter does it by itself whenever don ' t "use the" value any More)
For example, a data (such as a list in an example), it's a box, we assign it to a variable x, it's like putting a tag X on a box and then putting it on Y and using it to represent the data, but deleting the variable with del is like tearing the label labeled X. , leaving the label of Y.
Look at one more example:
Shoplist = [' Apple ', ' mango ', ' carrot ', ' banana ']
print (' The ' the ' I will be ' is ', shoplist[0 ]
Olditem = shoplist[0]
del shoplist[0] #del的是引用 instead of the object
print (' I bought the ', Olditem)
print (' My shop Ping the list is now ', shoplist)
Print (shoplist[0])
results in:
the "I", "I", "I", "I Bought" a Pple
My shopping list are now [' mango ', ' carrot ', ' banana ']
Mango

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.