python 中 del 的用法
2016-05-05 09:15
741 查看
python 中 del 的用法
用法一
用法二 转自 第七城市
首先介绍下:python的对象引用
1.1 列表内容:python不允许程序员选择采用传值还是传引用。Python参数传递采用的肯定是“传对象引用”的方式。实际上,这种方式相当于传值和传引用的一种综合。如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值——相当于通过“传引用”来传递对象。如果函数收到的是一个不可变对象(比如数字、字符或者元组)的引用,就不能直接修改原始对象——相当于通过“传值’来传递对象。
1.2 当人们复制列表或字典时,就复制了对象列表的引用同,如果改变引用的值,则修改了原始的参数。
1.3为了简化内存管理,Python通过引用计数机制实现自动垃圾回收功能,Python中的每个对象都有一个引用计数,用来计数该对象在不同场所分别被引用了多少次。每当引用一次Python对象,相应的引用计数就增1,每当消毁一次Python对象,则相应的引用就减1,只有当引用计数为零时,才真正从内存中删除Python对象。
del 删除引用而不是删除对象,对象由自动垃圾回收机制删除
看这个例子:
可以看到x和y指向同一个列表,但是删除x后,y并没有受到影响。这是为什么呢?
The reason for this is that you only delete the name,not the list itself,In fact ,there is no way to delete values in python(and you don’t really need to because the python interpreter does it by itself whenever you don’t use the value anymore)
举个例子,一个数据(比如例子中的列表),就是一个盒子,我们把它赋给一个变量x,就是好像把一个标签x贴到了盒子上,然后又贴上了y,用它们来代表这个数据,但是用del删除这个变量x就像是把标有x的标签给撕了,剩下了y的标签。
再看一个例子:
shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]
print (‘The first item I will buy is’, shoplist[0])
olditem = shoplist[0]
del shoplist[0] #del的是引用,而不是对象
print (‘I bought the’,olditem)
print (‘My shopping list is now’, shoplist)
print(shoplist[0])
结果为:
The first item I will buy is apple
I bought the apple
My shopping list is now [‘mango’, ‘carrot’, ‘banana’]
mango
用法一
>>> a = [1, "two", 3, "four"] >>> del a[0] #删除列表a中,下标为0的元素 >>> a ['two', 3, 'four'] >>> a.append("five") >>> a.append(6) >>> a ['two', 3, 'four', 'five', 6] >>> del a[2:4] #删除a从下标为2到4的元素,含头不含尾 >>> a ['two', 3, 6] >>> del a #删除列表a >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined
用法二 转自 第七城市
首先介绍下:python的对象引用
1.1 列表内容:python不允许程序员选择采用传值还是传引用。Python参数传递采用的肯定是“传对象引用”的方式。实际上,这种方式相当于传值和传引用的一种综合。如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值——相当于通过“传引用”来传递对象。如果函数收到的是一个不可变对象(比如数字、字符或者元组)的引用,就不能直接修改原始对象——相当于通过“传值’来传递对象。
1.2 当人们复制列表或字典时,就复制了对象列表的引用同,如果改变引用的值,则修改了原始的参数。
1.3为了简化内存管理,Python通过引用计数机制实现自动垃圾回收功能,Python中的每个对象都有一个引用计数,用来计数该对象在不同场所分别被引用了多少次。每当引用一次Python对象,相应的引用计数就增1,每当消毁一次Python对象,则相应的引用就减1,只有当引用计数为零时,才真正从内存中删除Python对象。
del 删除引用而不是删除对象,对象由自动垃圾回收机制删除
看这个例子:
>>> x = 1 >>> del x >>> x Traceback (most recent call last): 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 call last): File "<pyshell#12>", line 1, in <module> x NameError: name 'x' is not defined >>> y ['Hello', 'world'] >>>
可以看到x和y指向同一个列表,但是删除x后,y并没有受到影响。这是为什么呢?
The reason for this is that you only delete the name,not the list itself,In fact ,there is no way to delete values in python(and you don’t really need to because the python interpreter does it by itself whenever you don’t use the value anymore)
举个例子,一个数据(比如例子中的列表),就是一个盒子,我们把它赋给一个变量x,就是好像把一个标签x贴到了盒子上,然后又贴上了y,用它们来代表这个数据,但是用del删除这个变量x就像是把标有x的标签给撕了,剩下了y的标签。
再看一个例子:
shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]
print (‘The first item I will buy is’, shoplist[0])
olditem = shoplist[0]
del shoplist[0] #del的是引用,而不是对象
print (‘I bought the’,olditem)
print (‘My shopping list is now’, shoplist)
print(shoplist[0])
结果为:
The first item I will buy is apple
I bought the apple
My shopping list is now [‘mango’, ‘carrot’, ‘banana’]
mango
相关文章推荐
- python学习要点
- 使用QQ邮箱发送email(Python)
- python在引用其他文件夹下自定义函数
- python抓取性感尤物美女图
- python之路之正则表达式
- 模块间的调用
- python2.7.x的字符串编码到底什么鬼?(中文和英文的处理)
- python 大小写转换函数
- 【Python】【辅助程序】练手小程序:记录外网动态IP地址
- Python 迭代器和生成器
- 关于python的for语句
- libpython2.7.so.1.0 cannot open的解决方法
- 使用Python判断质数(素数)的简单方法讲解
- Python环境下搭建属于自己的pip源的教程
- Python使用Paramiko模块编写脚本进行远程服务器操作
- Python中内建函数的简单用法说明
- Python核心编程-第五章
- python 错误、异常
- python 测试与调试(test)
- Python爬虫: 抓取One网页上的每日一话和图