Re: [Tutor] python closures

2010-01-27 Thread Eike Welk
Hello Denis! On Monday November 30 2009 11:24:45 spir wrote: > which seems to indicate python really embeds "symbolic references" (*) to > outer *variables*, when creating a closure for g0. Not "pointer > references" (**), otherwise the replacement of x would not be seen by the > closure --like

Re: [Tutor] python closures

2009-12-01 Thread Hugo Arts
On Tue, Dec 1, 2009 at 8:42 AM, spir wrote: > Great example, thank you. > > By the way, do you know the idiom: > > def makeInc(start): >   def inc(): >      inc.n += 1 >      print inc.n >   inc.n = start >   # 'start' may change now >   # ... >   return inc > > inc= makeInc(start=3) > inc() > > I

Re: [Tutor] python closures

2009-12-01 Thread spir
May I suggest that upvalues are analog to parameters passed by name? (which is indeed not Python's paradigm) Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or c

Re: [Tutor] python closures

2009-12-01 Thread spir
Dave Angel dixit: > Maybe a more complex example might show the various linkages. > > glob = 42 > > def outer(parm1): > free = 12 > free3 = 19 > def inner(parm2, parm3=free3): > print "global", glob, ", free vars", parm1, free, free3, ", > locals", parm2, parm3 > free =

Re: [Tutor] python closures

2009-11-30 Thread Kent Johnson
On Mon, Nov 30, 2009 at 7:50 PM, Dave Angel wrote: > And this > example could be made more complex if outer() is a generator, in which case > it may not have actually ended when inner gets called. Indeed. In [8]: def gen(): ...: for i in range(5): ...: def inner(): ...:

Re: [Tutor] python closures

2009-11-30 Thread Dave Angel
Alan Gauld wrote: "spir" wrote I did wonder if you would need n=n but I didn't think you would need x=x. Its an interesting example and I confess I don't fully understand how Python's naming/reference rules are working here. to let the inner func g0 "remember" outer values. Why is this id

Re: [Tutor] python closures

2009-11-30 Thread spir
Eike Welk dixit: > On Monday 30 November 2009, Hugo Arts wrote: > > Consider this python session: > > >>> x = 0 > > >>> def f(): > > > > x = x + 1 > > > > >>> f() > > > > Traceback (most recent call last): > > File "", line 1, in > > f() > > File "", line 2, in f >

Re: [Tutor] python closures

2009-11-30 Thread Alan Gauld
"spir" wrote x = 1 def f(): n = 1 def g0(a): print (x + n + a) return g0 I'm surprised the snippet below works as expected (py 2.6) without any trick: I'm not sure how else it could work. x is a global name so the function must reference it. n is a local name so it musdt evaluate i

Re: [Tutor] python closures

2009-11-30 Thread Eike Welk
On Monday 30 November 2009, Hugo Arts wrote: > Consider this python session: > >>> x = 0 > >>> def f(): > > x = x + 1 > > >>> f() > > Traceback (most recent call last): > File "", line 1, in > f() > File "", line 2, in f > x = x + 1 > UnboundLocalError: local variable

Re: [Tutor] python closures

2009-11-30 Thread Hugo Arts
On Mon, Nov 30, 2009 at 4:16 PM, Kent Johnson wrote: > That has not been needed since 2.1 though it is still useful when > closures are created in a loop (because closures are kind of late > bound - I'm not sure the exact technical explanation): > In [13]: def f(): >   :     l = [] >   :

Re: [Tutor] python closures

2009-11-30 Thread Hugo Arts
On Mon, Nov 30, 2009 at 11:24 AM, spir wrote: > Hello, > > Below startup definitions: > > x = 1 > > def f(): >  n = 1 >  def g0(a): >    print (x + n + a) >  return g0 > > > I'm surprised the snippet below works as expected (py 2.6) without any trick: > > g = f() > g(1)    # --> 3 > > This means a

Re: [Tutor] python closures

2009-11-30 Thread Stefan Behnel
spir, 30.11.2009 11:24: > Below startup definitions: > > x = 1 > > def f(): > n = 1 > def g0(a): > print (x + n + a) > return g0 > > I'm surprised the snippet below works as expected (py 2.6) without any trick: > > g = f() > g(1) # --> 3 > > This means a (real) closure is built for

Re: [Tutor] python closures

2009-11-30 Thread Kent Johnson
On Mon, Nov 30, 2009 at 5:24 AM, spir wrote: > Hello, > > Below startup definitions: > > x = 1 > > def f(): >  n = 1 >  def g0(a): >    print (x + n + a) >  return g0 > > > I'm surprised the snippet below works as expected (py 2.6) without any trick: > > g = f() > g(1)    # --> 3 > > This means a

Re: [Tutor] python closures

2009-11-30 Thread Wayne Werner
On Mon, Nov 30, 2009 at 4:24 AM, spir wrote: > which seems to indicate python really embeds "symbolic references" (*) to > outer *variables*, when creating a closure for g0. Not "pointer references" > (**), otherwise the replacement of x would not be seen by the closure --like > in the case of de

[Tutor] python closures

2009-11-30 Thread spir
Hello, Below startup definitions: x = 1 def f(): n = 1 def g0(a): print (x + n + a) return g0 I'm surprised the snippet below works as expected (py 2.6) without any trick: g = f() g(1)# --> 3 This means a (real) closure is built for g0, or what? Thought I would need instead to