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

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

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:
    585
    Steven Bethard
    Mar 3, 2006
  2. Replies:
    2
    Views:
    326
  3. mrstephengross
    Replies:
    3
    Views:
    564
    James Kanze
    May 10, 2007
  4. Yansky
    Replies:
    4
    Views:
    475
    Yansky
    Dec 13, 2007
  5. Larry Bates

    del and sets proposal

    Larry Bates, Oct 2, 2008, in forum: Python
    Replies:
    21
    Views:
    739
  6. Albert Hopkins

    When (and why) to use del?

    Albert Hopkins, Dec 9, 2008, in forum: Python
    Replies:
    5
    Views:
    348
  7. Hicham Mouline
    Replies:
    2
    Views:
    923
    Juha Nieminen
    Sep 1, 2009
  8. xdevel1999

    read DEL and BACK key from stdin

    xdevel1999, Dec 9, 2011, in forum: Java
    Replies:
    2
    Views:
    428
    Roedy Green
    Dec 9, 2011
Loading...