0
这段代码为什么会这样工作?为什么del(x)在变量名称周围带括号?
x = 3
print(dir()) #output indicates that x is defined in the global scope
del (x)
print(dir()) #output indicates that x is not defined in the global scope
我的理解是,del
是一个Python关键字,而接下来del
应该是一个名字。 (name)
不是一个名字。为什么该示例似乎显示del (name)
与del name
的作用相同?
接下来'del'并不总是一个名字;考虑'del arr [n]'或'del obj.attr'。 – trentcl
您可以在任何表达式周围放置括号,并且该值与表达式相同。例如。 'foo = x'或'foo =(x)'是同样的事情。 – Barmar
'del(name)'被解释为'del(name)',并且括号被忽略。这就像在Python2.7中执行'print('name')',它被_interpreted_作为'print('name')'。 –