9

I am relatively new to Python development, and in reading through the language documentation, I came across a line that read:

It is illegal to unbind a name that is referenced by an enclosing scope; the compiler will report a SyntaxError.

So in a learning exercise, I am trying to create this error in the interactive shell, but I haven't been able to find a way to do so. I am using Python v2.7.3, so using the nonlocal keyword like

def outer():
 a=5
 def inner():
 nonlocal a
 print(a)
 del a

is not an option, and without using nonlocal, when Python sees del a in the inner function, it interprets it as a local variable that has not been bound and throws an UnboundLocalError exception.

Obviously there is an exception to this rule with regards to global variables, so how can I create a situation where I am "illegally" unbinding a variable name that is being referenced by an enclosing scope?

asked Mar 11, 2013 at 15:33

2 Answers 2

10

The deletion has to take place in the outer scope:

>>> def foo():
... a = 5
... def bar():
... return a
... del a
... 
SyntaxError: can not delete variable 'a' referenced in nested scope

The compile-time restriction has been removed in Python 3:

$ python3.3
Python 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
... a = 5
... def bar():
... return a
... del a
... return bar
... 
>>>

Instead, a NameError is raised when you try to refer to a:

>>> foo()()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<stdin>", line 4, in bar
NameError: free variable 'a' referenced before assignment in enclosing scope

I am tempted to file a documentation bug here. For Python 2, the documentation is misleading; it is deleting a variable used in a nested scope that triggers the compile-time error, and the error is no longer raised at all in Python 3.

answered Mar 11, 2013 at 15:36
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, that makes much more sense, thank you. Agreed about the misleading Python 2 documentation in regards to this. The name that is referenced by an enclosing scope seems to imply (in my opinion anyways) a variable that is referenced in a scope enclosing the scope that you unbind the name in.
5

To trigger that error, you need to unbind the variable in the context of the outer scope.

>>> def outer():
... a=5
... del a
... def inner(): 
... print a
... 
SyntaxError: can not delete variable 'a' referenced in nested scope
answered Mar 11, 2013 at 15:37

1 Comment

Thank you very much, apparently the documentation is a bit misleading. This makes more sense.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.