Unbinding a name referenced by an enclosing scope

G

Grigory Javadyan

From the Python Language Reference (v 3.1):
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. Apparently, I'm not
understanding the rule correctly. Can anyone explain it? Thanks.
 
P

Peter Otten

Grigory said:
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. Apparently, I'm not
understanding the rule correctly. Can anyone explain it? Thanks.

The line you quote is probably meant to describe the following:
.... a = 42
.... def g():
.... nonlocal a
.... del a
....
SyntaxError: can not delete variable 'a' referenced in nested scope

Please file a documentation bug if you can come up with a clarification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top