intricated functions: how to share a variable
Chris Rebert
clp2 at rebertia.com
Wed Aug 5 06:42:23 EDT 2009
On Wed, Aug 5, 2009 at 3:13 AM, TP<Tribulations at paralleles.invalid> wrote:
> Hi everybody,
>
> See the following example:
>
> #########
> def tutu():
>
> def toto():
>
nonlocal a #note: this requires a rather recent version of python
> print a
> a = 4
> print a
>
> a=2
> toto()
>
> tutu()
> ##########
>
> I obtain the following error:
> "UnboundLocalError: local variable 'a' referenced before assignment"
>
> This is because Python looks in the local context before looking in the
> global context.
>
> The use of "global a" in toto() does not help because global allows to force
> Python to look for the variable at the module level.
>
> So, how to share a variable between intricated functions?
Details: http://www.python.org/dev/peps/pep-3104/
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list