unbinding a global variable in Python

D

Diez B. Roggisch

Mark said:
In Lisp this is done so

error: unbound variable

How is this done in Python?

Mark
Traceback (most recent call last):

Be aware of functions that declare global variables though:

.... global bar
.... bar = 10
....Traceback (most recent call last):
10


Diez
 
M

Mark Tarver

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:


...     global bar
...     bar = 10
...>>> foo()

Traceback (most recent call last):


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
 
P

Peter Otten

Mark said:
In Lisp this is done so

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top