del operator and global namespace
alust
alust at alust.homeunix.com
Wed Dec 8 13:06:01 EST 2010
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
More information about the Python-list
mailing list