我真的想不出Python需要del关键字的任何原因(而且大多数语言似乎都没有类似的关键字)。例如,与其删除变量,还不如将None赋值给它。当从字典中删除时,可以添加del方法。
在Python中保留del是有原因的吗,还是它是Python前垃圾收集时代的遗迹?
我真的想不出Python需要del关键字的任何原因(而且大多数语言似乎都没有类似的关键字)。例如,与其删除变量,还不如将None赋值给它。当从字典中删除时,可以添加del方法。
在Python中保留del是有原因的吗,还是它是Python前垃圾收集时代的遗迹?
当前回答
这是我贡献的2美分:
我有一个优化问题,我使用一个Nlopt库。 我初始化类和它的一些方法,我在代码的其他几个部分使用。
我得到的结果是随机的,即使是同样的数值问题。
我刚刚意识到,通过这样做,一些虚假数据包含在对象中,而它应该没有任何问题。使用del后,我猜内存被正确地清除,这可能是一个内部问题的类,其中一些变量可能不喜欢被重用没有适当的构造函数。
其他回答
首先,你可以删除除局部变量之外的其他东西
del list_item[4]
del dictionary["alpha"]
这两者显然都是有用的。其次,在局部变量上使用del使意图更加明确。比较:
del foo
to
foo = None
我知道在del foo的情况下,目的是从作用域中删除变量。foo = None是否这样做还不清楚。如果有人只是赋值foo = None,我可能会认为这是死代码。但我立刻就知道,编码del foo的人想要做什么。
del在python中什么时候有用?
您可以使用它来删除数组中的单个元素,而不是切片语法x[i:i+1]=[]。这可能是有用的,例如,如果你在操作系统。行走并希望删除目录中的一个元素。不过,我不认为关键字对此有用,因为可以使用[].remove(index)方法(.remove方法实际上是search-and-remove-first-instance-of-value)。
在以上回答的基础上补充几点: 德尔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可以用来做什么的例子,我发现它在这样的情况下很有用:
def f(a, b, c=3):
return '{} {} {}'.format(a, b, c)
def g(**kwargs):
if 'c' in kwargs and kwargs['c'] is None:
del kwargs['c']
return f(**kwargs)
# g(a=1, b=2, c=None) === '1 2 3'
# g(a=1, b=2) === '1 2 3'
# g(a=1, b=2, c=4) === '1 2 4'
这两个函数可以在不同的包/模块中,程序员不需要知道f中的参数c实际上有什么默认值。因此,通过将kwargs与del结合使用,您可以将其设置为None(或者在这种情况下也可以保留它),从而说“I want the default value on c”。
你也可以这样做:
def g(a, b, c=None):
kwargs = {'a': a,
'b': b}
if c is not None:
kwargs['c'] = c
return f(**kwargs)
然而,我发现前面的例子更加DRY和优雅。
另一个小众案例,但很有用。
from getpass import getpass
pass = getpass()
token = get_auth_token(pass)
del pass
# Assume more code here...
在删除pass变量之后,您不会冒它稍后被错误打印出来的风险,或者以其他方式结束在日志或堆栈跟踪中。