Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > del operator and global namespace

Reply
Thread Tools

del operator and global namespace

 
 
alust
Guest
Posts: n/a
 
      12-08-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
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      12-08-2010
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stream operator in namespace masks global stream operator mrstephengross C++ 3 05-10-2007 07:02 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
operator<<() in namespace or global? ryan_melville@xiotech.com C++ 2 08-14-2006 05:57 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. ASP .Net 1 07-29-2003 04:31 PM



Advertisments