我真的想不出Python需要del关键字的任何原因(而且大多数语言似乎都没有类似的关键字)。例如,与其删除变量,还不如将None赋值给它。当从字典中删除时,可以添加del方法。
在Python中保留del是有原因的吗,还是它是Python前垃圾收集时代的遗迹?
我真的想不出Python需要del关键字的任何原因(而且大多数语言似乎都没有类似的关键字)。例如,与其删除变量,还不如将None赋值给它。当从字典中删除时,可以添加del方法。
在Python中保留del是有原因的吗,还是它是Python前垃圾收集时代的遗迹?
当前回答
由于我还没有看到交互式控制台的答案,我将展示一个。
当foo=None时,该引用和对象存在,它不指向它。
而del foo也会销毁对象和引用。
如果你这样做如果foo是None并且它被删除了它就会升起NameError作为引用,它的对象所有介于两者之间的东西都会被del删除
删除目标列表会递归地从左到右删除每个目标。
与此同时,foo=None只是一个指向None的引用,因此引用仍然是有效的,对象也是如此。
[…在Python中,变量是对象的引用,任何变量都可以引用任何对象[…]
链接到引用1
链接到引用2
其他回答
"del"命令对于控制数组中的数据非常有用,例如:
elements = ["A", "B", "C", "D"]
# Remove first element.
del elements[:1]
print(elements)
输出:
[' b ', ' c ', ' d ']
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 :)
我想详细说明公认的答案,以强调将变量设置为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'没有定义。
使用numpy.load后强制关闭文件:
这可能是一个小众的用法,但我发现它在使用numpy时很有用。加载以读取文件。每隔一段时间,我就会更新文件,需要复制一个同名的文件到目录中。
我使用del来释放文件,并允许我复制新文件。
注意,我想避免使用with context管理器,因为我在命令行上玩图,不想经常按tab键!
看这个问题。
还有一个小众用途: 在带有ROOT5或ROOT6的pyroot中,"del"可以用于删除引用不再存在的c++对象的python对象。这允许pyroot的动态查找找到同名的c++对象,并将其绑定到python名称。所以你可以有这样一个场景:
import ROOT as R
input_file = R.TFile('inputs/___my_file_name___.root')
tree = input_file.Get('r')
tree.Draw('hy>>hh(10,0,5)')
R.gPad.Close()
R.hy # shows that hy is still available. It can even be redrawn at this stage.
tree.Draw('hy>>hh(3,0,3)') # overwrites the C++ object in ROOT's namespace
R.hy # shows that R.hy is None, since the C++ object it pointed to is gone
del R.hy
R.hy # now finds the new C++ object
希望ROOT7的更健全的对象管理能够填补这个空缺。