a = 3def x(): global a
del(a)print(a)x()在python2.7中執行上面這段代碼并沒有問題!但是在python2.7的文檔中(沒有錨記,大概在第10段)有這樣一句話:It is illegal to unbind a name referenced by an enclosing scope; the compiler will report a SyntaxError.我在SO上也看到了同樣的一個提問,但是它答案給出的測試代碼是這樣的:>>> def outer():... a=5... def inner():... nonlocal a... print(a)... del aSyntaxError: can not delete variable 'a' referenced in nested scope但是在python2.7中,并沒有nonlocal這個關鍵字(事實上,我在3.2上測試上面這段代碼也是沒有問題的)。
我想知道,如果這文檔(2.7)上這句話是正確的,那么測試代碼(2.7)是怎樣的?
慕尼黑8549860
2023-04-16 21:17:42