unbinding a global variable in Python

Diez B. Roggisch deets at nospam.web.de
Thu Apr 30 13:36:24 CEST 2009


Mark Tarver wrote:

> In Lisp this is done so
> 
>> (setq *g* 0)
> 0
> 
>> *g*
> 0
> 
>> (makunbound '*g*)
> *g*
> 
>> *g*
> error: unbound variable
> 
> How is this done in Python?
> 
> Mark

>>> foo = "bar"
>>> del foo
>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> 

Be aware of functions that declare global variables though:

                            
>>> def foo():
...     global bar
...     bar = 10
...
>>> foo()
>>> bar
10
>>> del bar
>>> bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'bar' is not defined
>>> foo()
>>> bar
10


Diez



More information about the Python-list mailing list