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')'。 –