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

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


当前回答

I think one of the reasons that del has its own syntax is that replacing it with a function might be hard in certain cases given it operates on the binding or variable and not the value it references. Thus if a function version of del were to be created a context would need to be passed in. del foo would need to become globals().remove('foo') or locals().remove('foo') which gets messy and less readable. Still I say getting rid of del would be good given its seemingly rare use. But removing language features/flaws can be painful. Maybe python 4 will remove it :)

其他回答

del所做的部分(来自Python语言参考):

删除名称将从本地或全局名称空间删除该名称的绑定

将None赋值给名称不会删除名称与名称空间的绑定。

(我想关于移除名称绑定是否真的有用可能会有一些争论,但这是另一个问题。)

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在python中什么时候有用?

您可以使用它来删除数组中的单个元素,而不是切片语法x[i:i+1]=[]。这可能是有用的,例如,如果你在操作系统。行走并希望删除目录中的一个元素。不过,我不认为关键字对此有用,因为可以使用[].remove(index)方法(.remove方法实际上是search-and-remove-first-instance-of-value)。

只是另一种想法。

当在Django这样的框架中调试http应用程序时,调用堆栈中充满了以前使用过的无用和混乱的变量,特别是当它是一个非常长的列表时,这对开发人员来说是非常痛苦的。因此,在这一点上,名称空间控制可能是有用的。

我发现在使用Numpy处理大数据时,del对于伪手动内存管理非常有用。例如:

for image_name in large_image_set:
    large_image = io.imread(image_name)
    height, width, depth = large_image.shape
    large_mask = np.all(large_image == <some_condition>)
    # Clear memory, make space
    del large_image; gc.collect()

    large_processed_image = np.zeros((height, width, depth))
    large_processed_image[large_mask] = (new_value)
    io.imsave("processed_image.png", large_processed_image)

    # Clear memory, make space
    del large_mask, large_processed_image; gc.collect()

当Python GC无法跟上时,系统会疯狂地切换,这可能会导致脚本停止,而它在宽松的内存阈值下运行得非常流畅,从而在机器工作时留下了足够的空间来使用机器浏览和编码。