variable visiblity
Gerhard Haering
gh at ghaering.de
Wed Apr 16 09:44:56 EDT 2003
Gabor Nagy wrote:
> Hi!
>
> How can I access the "parent" function's local variable from a nested
> function?
>
> example:
> def foo(par):
> def f(x): print x, par
> f(0)
>
> SyntaxWarning: local name 'par' in 'foo' shadows use of 'par' as global in
> nested scope 'f'
> when run, prints x, then
> ...in f NameError: global name 'par' is not defined
You're using Python 2.1. Nested scopes were introduced with Python 2.1,
but not enabled by default, so you'll need to put a
from __future__ import nested_scopes
on the top of your module.
Or just upgrade to Python 2.2, where nested scopes are enabled by
default :-)
-- Gerhard
More information about the Python-list
mailing list