Unbinding multiple variables

Steven Bethard steven.bethard at gmail.com
Fri Jan 21 14:37:25 EST 2005


Johnny Lin wrote:
> my understanding about locals() from the nutshell book was that i
> should treat that dictionary as read-only.  is it safe to use it to
> delete entries?

No it's not:

py> def f():
...     x = 1
...     del locals()['x']
...     print x
...
py> f()
1
py> def f():
...     x = 1
...     del x
...     print x
...
py> f()
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
   File "<interactive input>", line 4, in f
UnboundLocalError: local variable 'x' referenced before assignment

Steve



More information about the Python-list mailing list