unbinding a global variable in Python

Mark Tarver dr.mtarver at ukonline.co.uk
Thu Apr 30 13:43:22 CEST 2009


On 30 Apr, 12:36, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> 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- Hide quoted text -
>
> - Show quoted text -

Great; and how can I test to see if a global is bound?

e.g Lisp

> (setq *g* 0)
0

> (boundp '*g*)
t

Mark



More information about the Python-list mailing list