为什么这段代码会这样工作?
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陈述 https://docs.python.org/3/reference/simple_stmts.html#the-del-statement is:
del_stmt ::= "del" target_list
并从定义target_list https://docs.python.org/3/reference/simple_stmts.html#assignment-statements:
target_list ::= target ("," target)* [","]
target ::= identifier
| "(" target_list ")"
| "[" [target_list] "]"
| ...
您可以看到目标列表周围的括号是允许的。
例如,如果您定义x,y = 1,2
,所有这些都是允许的并且具有相同的效果:
del x,y
del (x,y)
del (x),[y]
del [x,(y)]
del ([x], (y))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)