我真的想不出Python需要del关键字的任何原因(而且大多数语言似乎都没有类似的关键字)。例如,与其删除变量,还不如将None赋值给它。当从字典中删除时,可以添加del方法。
在Python中保留del是有原因的吗,还是它是Python前垃圾收集时代的遗迹?
我真的想不出Python需要del关键字的任何原因(而且大多数语言似乎都没有类似的关键字)。例如,与其删除变量,还不如将None赋值给它。当从字典中删除时,可以添加del方法。
在Python中保留del是有原因的吗,还是它是Python前垃圾收集时代的遗迹?
当前回答
在以上回答的基础上补充几点: 德尔x
x的定义表示r -> o(一个引用r指向一个对象o),但del x改变的是r而不是o。这是一个对对象的引用(指针)的操作,而不是与x相关的对象。区分r和o是这里的关键。
It removes it from locals(). Removes it from globals() if x belongs there. Removes it from the stack frame (removes the reference physically from it, but the object itself resides in object pool and not in the stack frame). Removes it from the current scope. It is very useful to limit the span of definition of a local variable, which otherwise can cause problems. It is more about declaration of the name rather than definition of content. It affects where x belongs to, not where x points to. The only physical change in memory is this. For example if x is in a dictionary or list, it (as a reference) is removed from there(and not necessarily from the object pool). In this example, the dictionary it belongs is the stack frame (locals()), which overlaps with globals().
其他回答
首先,你可以删除除局部变量之外的其他东西
del list_item[4]
del dictionary["alpha"]
这两者显然都是有用的。其次,在局部变量上使用del使意图更加明确。比较:
del foo
to
foo = None
我知道在del foo的情况下,目的是从作用域中删除变量。foo = None是否这样做还不清楚。如果有人只是赋值foo = None,我可能会认为这是死代码。但我立刻就知道,编码del foo的人想要做什么。
Del经常出现在__init__.py文件中。任何在__init__.py文件中定义的全局变量都将自动“导出”(它将包含在from模块import *中)。避免这种情况的一种方法是定义__all__,但这可能会很混乱,并不是每个人都使用它。
例如,如果你在__init__.py中有这样的代码
import sys
if sys.version_info < (3,):
print("Python 2 not supported")
然后您的模块将导出sys名称。你应该写
import sys
if sys.version_info < (3,):
print("Python 2 not supported")
del sys
Del删除变量及其所指向的对象的绑定。
>>> a = ['a', 'b', 'c']
>>> b = a
>>> del a
>>> b
['a', 'b', 'c']
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
我能想到的一个简单的用例是,如果你已经使用内置函数名作为变量,并且你想在它已经被你的变量名“覆盖”之后使用该函数。
t = ('a', "letter")
value, type = t
print(value, type)
del type
print(type(value))
输出:
a letter
<class 'str'>
我想详细说明公认的答案,以强调将变量设置为None与使用del删除变量之间的细微差别:
给定变量foo = 'bar',函数定义如下:
def test_var(var):
if var:
print('variable tested true')
else:
print('variable tested false')
一旦初始声明,test_var(foo)产生的变量测试为true。
现在试一试:
foo = None
test_var(foo)
它产生的变量测试为假。
将这种行为与以下行为进行对比:
del foo
test_var(foo)
现在会引发NameError: name 'foo'没有定义。
在以上回答的基础上补充几点: 德尔x
x的定义表示r -> o(一个引用r指向一个对象o),但del x改变的是r而不是o。这是一个对对象的引用(指针)的操作,而不是与x相关的对象。区分r和o是这里的关键。
It removes it from locals(). Removes it from globals() if x belongs there. Removes it from the stack frame (removes the reference physically from it, but the object itself resides in object pool and not in the stack frame). Removes it from the current scope. It is very useful to limit the span of definition of a local variable, which otherwise can cause problems. It is more about declaration of the name rather than definition of content. It affects where x belongs to, not where x points to. The only physical change in memory is this. For example if x is in a dictionary or list, it (as a reference) is removed from there(and not necessarily from the object pool). In this example, the dictionary it belongs is the stack frame (locals()), which overlaps with globals().