Unbinding a name referenced by an enclosing scope

Discussion in 'Python' started by Grigory Javadyan, Feb 20, 2011.

  1. >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.
    Grigory Javadyan, Feb 20, 2011
    #1
    1. Advertising

  2. Grigory Javadyan

    Peter Otten Guest

    Grigory Javadyan wrote:

    >>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.


    The line you quote is probably meant to describe the following:

    >>> def f():

    .... 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.
    Peter Otten, Feb 20, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sin

    Re: UNbinding a socket

    Sin, Jun 23, 2003, in forum: C++
    Replies:
    2
    Views:
    13,748
  2. Michael Furman

    Re: (OT) UNbinding a socket

    Michael Furman, Jun 23, 2003, in forum: C++
    Replies:
    0
    Views:
    1,388
    Michael Furman
    Jun 23, 2003
  3. Richard Spooner

    Unbinding sockets in threads..

    Richard Spooner, Aug 3, 2004, in forum: Python
    Replies:
    0
    Views:
    326
    Richard Spooner
    Aug 3, 2004
  4. Christopher J. Bottaro
    Replies:
    8
    Views:
    155
    Christopher J. Bottaro
    Apr 2, 2008
  5. Yakov
    Replies:
    1
    Views:
    91
    John W. Krahn
    Nov 12, 2006
Loading...

Share This Page