我真的想不出Python需要del关键字的任何原因(而且大多数语言似乎都没有类似的关键字)。例如,与其删除变量,还不如将None赋值给它。当从字典中删除时,可以添加del方法。

在Python中保留del是有原因的吗,还是它是Python前垃圾收集时代的遗迹?


当前回答

删除变量与将其设置为None不同

使用del删除变量名可能很少使用,但如果没有关键字,这是无法实现的。如果你可以通过写a=1来创建一个变量名,那么理论上你可以通过删除a来撤销这一点。

在某些情况下,它可以使调试更容易,因为试图访问已删除的变量将引发NameError。

可以删除类实例属性

Python允许你编写如下代码:

class A(object):
    def set_a(self, a):
        self.a=a
a=A()
a.set_a(3)
if hasattr(a, "a"):
    print("Hallo")

如果选择向类实例动态添加属性,当然希望能够通过写入来撤销它

del a.a

其他回答

另一个小众案例,但很有用。

from getpass import getpass

pass = getpass()
token = get_auth_token(pass)
del pass

# Assume more code here...

在删除pass变量之后,您不会冒它稍后被错误打印出来的风险,或者以其他方式结束在日志或堆栈跟踪中。

当你使用sys.exc_info()检查异常时,有一个特定的例子说明你应该使用del(可能还有其他的例子,但我知道这个是现成的)。这个函数返回一个元组、引发的异常类型、消息和一个回溯。

前两个值通常足以诊断错误并对其进行处理,但第三个值包含从引发异常的位置到捕获异常的位置之间的整个调用堆栈。特别是,如果你做

try:
    do_evil()
except:
    exc_type, exc_value, tb = sys.exc_info()
    if something(exc_value):
        raise

回溯,TB最终在调用堆栈的局部变量中,创建了一个不能被垃圾收集的循环引用。因此,重要的是要做到:

try:
    do_evil()
except:
    exc_type, exc_value, tb = sys.exc_info()
    del tb
    if something(exc_value):
        raise

打破循环引用。在许多情况下,您希望调用sys.exc_info(),就像使用元类魔法一样,回溯是有用的,因此您必须确保在可能离开异常处理程序之前清除它。如果你不需要回溯,你应该立即删除它,或者直接执行:

exc_type, exc_value = sys.exc_info()[:2]

一起避免这一切。

我想详细说明公认的答案,以强调将变量设置为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'没有定义。

除非重新初始化,否则Del将从当前作用域删除变量。将其设置为None将其保留在当前范围内。

a = "python string"        
print(a)
del a
print(a)
a = "new python string"
print(a)

输出:

python string
Traceback (most recent call last):
  File "testing.py", line 4, in <module>
    print(a)
NameError: name 'a' is not defined

在以上回答的基础上补充几点: 德尔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().