unbinding a global variable in Python

Peter Otten __peter__ at web.de
Thu Apr 30 15:15:05 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?

Often it is a better choice to initialize the global with a sentinel:

g = None

# ...
g = "something meaningful"

# the equivalent of checking whether it's bound:
if g is not None:
   # use g

# the eqivalent of unbinding:
g = None

Peter










More information about the Python-list mailing list