del operator and global namespace

Discussion in 'Python' started by alust, Dec 8, 2010.

  1. alust

    alust Guest

    Hello,

    Can somebody explain this strange (to me) effect please.

    In this program it is impossible to access a global variable within a
    function:

    $ cat /tmp/test.py
    x='xxx'
    def f():
    print x
    del x

    f()

    $ python /tmp/test.py
    Traceback (most recent call last):
    File "/tmp/test.py", line 6, in <module>
    f()
    File "/tmp/test.py", line 3, in f
    print x
    UnboundLocalError: local variable 'x' referenced before assignment

    But if we comment the del operator the program will work:

    $ cat /tmp/test.py
    x='xxx'
    def f():
    print x
    #del x

    f()

    $ python /tmp/test.py
    xxx

    So why in this example the print operator is influenced by del
    operator
    that should be executed after it?

    --
    Thanks, Alexei
    alust, Dec 8, 2010
    #1
    1. Advertising

  2. alust

    MRAB Guest

    On 08/12/2010 18:06, alust wrote:
    > Hello,
    >
    > Can somebody explain this strange (to me) effect please.
    >
    > In this program it is impossible to access a global variable within a
    > function:
    >
    > $ cat /tmp/test.py
    > x='xxx'
    > def f():
    > print x
    > del x
    >
    > f()
    >
    > $ python /tmp/test.py
    > Traceback (most recent call last):
    > File "/tmp/test.py", line 6, in<module>
    > f()
    > File "/tmp/test.py", line 3, in f
    > print x
    > UnboundLocalError: local variable 'x' referenced before assignment
    >
    > But if we comment the del operator the program will work:
    >
    > $ cat /tmp/test.py
    > x='xxx'
    > def f():
    > print x
    > #del x
    >
    > f()
    >
    > $ python /tmp/test.py
    > xxx
    >
    > So why in this example the print operator is influenced by del
    > operator
    > that should be executed after it?
    >

    The Python source code is compiled to bytecodes which are then
    interpreted. It's during the compilation stage that it determines
    whether a name is local. If you bind to a name:

    x = 0

    or del a name:

    del x

    anywhere in the function, it takes that name to be local.

    When it actually interprets the bytecode at the print statement it
    tries to reference the name, but nothing has been bound to it yet, so
    it raises an exception.
    MRAB, Dec 8, 2010
    #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. Replies:
    5
    Views:
    405
    Steven Bethard
    Mar 3, 2006
  2. Replies:
    2
    Views:
    271
  3. mrstephengross
    Replies:
    3
    Views:
    389
    James Kanze
    May 10, 2007
  4. Yansky
    Replies:
    4
    Views:
    386
    Yansky
    Dec 13, 2007
  5. Larry Bates

    del and sets proposal

    Larry Bates, Oct 2, 2008, in forum: Python
    Replies:
    21
    Views:
    612
Loading...

Share This Page