On Wed, 2009-08-05 at 12:39 +0200, Diez B. Roggisch wrote:
> Another often used trick is to have a mutable container-object, like this:
>
> def tutu():
>a = [2]
>
>def toto():
>a[0] = 4
>
>toto()
When you need a writable bound variable inside a closure, I prefer this
idi
TP wrote:
> Then, as advised Diez, perhaps the best solution is to have "true" global
> variables by using a class and defining the variable a as a member self.a
> of the class. By doing like this, a will be known everywhere.
Or, as Bearophile suggested, you could use the standard way of passing
Bearophile wrote:
> So probably a better solution is to just use the normal function
> semantics: you pass them an argument and you take an argument as
> return value. Such return value will be the new version of the value
> you talk about.
Thanks for your answer.
Yes, it is better like this. My
TP:
> def tutu():
>
> def toto():
>
> 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
TP wrote:
> Hi everybody,
>
> See the following example:
>
> #
> def tutu():
>
> def toto():
>
> print a
> a = 4
> print a
>
> a=2
> toto()
>
> tutu()
> ##
>
> I obtain the following error:
> "UnboundLocalError: local variable 'a' referenc
On Wed, Aug 5, 2009 at 3:13 AM, TP 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()
>
TP wrote:
> Hi everybody,
>
> See the following example:
>
> #
> def tutu():
>
> def toto():
>
> print a
> a = 4
> print a
>
> a=2
> toto()
>
> tutu()
> ##
>
> I obtain the following error:
> "UnboundLocalError: local variable 'a' referenc
Hi everybody,
See the following example:
#
def tutu():
def toto():
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 i