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
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
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
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 =
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():
...:
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
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
>
"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
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
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 = []
> :
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
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
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
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
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
15 matches
Mail list logo