Unbinding a name referenced by an enclosing scope - error in documentation?
Hi, guys I'm not sure if python-dev is the right place to write to, but I'm really curious about this:
From the Python Language reference: It is illegal to unbind a name referenced by an enclosing scope; the compiler will report a SyntaxError.
But when I run the following code: a = 3 def x(): global a del(a) print(a) x() it works fine; and when I change the order of calls: x() print(a) I get a NameError, not a SyntaxError. Now I asked the same question on python-list and people suggested that the true meaning of that rule is:
def f(): ... a = 42 ... def g(): ... nonlocal a ... del a ... SyntaxError: can not delete variable 'a' referenced in nested scope
Which looks weird, because the name is referenced in the _enclosed_ scope, not the _enclosing_ scope. Is there a typo in the documentation or am I missing something?
2011/2/26 Grigory Javadyan
def f(): ... a = 42 ... def g(): ... nonlocal a ... del a ... SyntaxError: can not delete variable 'a' referenced in nested scope
Which looks weird, because the name is referenced in the _enclosed_ scope, not the _enclosing_ scope. Is there a typo in the documentation or am I missing something?
Actually you can do that now 3.2+. I've now removed that sentence;. -- Regards, Benjamin
From: Grigory Javadyan
... def f(): ... a = 42 ... def g(): ... nonlocal a ... del a ... SyntaxError: can not delete variable 'a' referenced in nested scope
Is there a rational for this? It seems inconsistent -- if you can assign to names in outer scopes, you should be able to del them as well. -- Greg This email may be confidential and subject to legal privilege, it may not reflect the views of the University of Canterbury, and it is not guaranteed to be virus free. If you are not an intended recipient, please notify the sender immediately and erase all copies of the message and any attachments. Please refer to http://www.canterbury.ac.nz/emaildisclaimer for more information.
2011/2/26 Greg Ewing
From: Grigory Javadyan
... def f(): ... a = 42 ... def g(): ... nonlocal a ... del a ... SyntaxError: can not delete variable 'a' referenced in nested scope
Is there a rational for this? It seems inconsistent -- if you can assign to names in outer scopes, you should be able to del them as well.
Notice that it's now been changed... -- Regards, Benjamin
participants (3)
-
Benjamin Peterson
-
Greg Ewing
-
Grigory Javadyan