Re: intricated functions: how to share a variable

2009-08-05 Thread Jason Tackaberry
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

Re: intricated functions: how to share a variable

2009-08-05 Thread alex23
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

Re: intricated functions: how to share a variable

2009-08-05 Thread TP
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

Re: intricated functions: how to share a variable

2009-08-05 Thread Bearophile
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

Re: intricated functions: how to share a variable

2009-08-05 Thread Peter Otten
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

Re: intricated functions: how to share a variable

2009-08-05 Thread Chris Rebert
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() >

Re: intricated functions: how to share a variable

2009-08-05 Thread Diez B. Roggisch
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

intricated functions: how to share a variable

2009-08-05 Thread TP
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