Cancel the name referenced by the closing area

From the Python language reference (v 3.1 see here - http://docs.python.org/py3k/reference/executionmodel.html#naming-and-binding ):

It is incorrect to undo the name referenced by the closing region; the compiler will report a SyntaxError error.

But when I run the following code:

a = 3 def x(): global a del(a) print(a) x() 

it works great; and when I change the order of calls:

 x() print(a) 

I get a NameError, not a SyntaxError. Apparently, I misunderstand the rule. Can anyone explain this? Thank you

+6
python syntax-error unbind
source share
2 answers

I contacted the people on the python-devel list, and here is what I got:

In fact, you can do it now 3.2+. Now I deleted this sentence.

So actually it was a documentation error.

+3
source share

I do not think this rule applies to global coverage. Global reach is always fully available.

Here is an example:

 >>> def outer(): ... a=5 ... def inner(): ... nonlocal a ... print(a) ... del a ... SyntaxError: can not delete variable 'a' referenced in nested scope 
+4
source share

All Articles