[Tutor] List comprehension + lambdas - strange behaviour
spir ☣
denis.spir at gmail.com
Fri May 7 09:11:38 CEST 2010
On Thu, 6 May 2010 22:15:34 +0100
"Alan Gauld" <alan.gauld at btinternet.com> wrote:
> As others have pointed out you are returning a reference not a value.
Yes. (I have said that, too.) But still there is a mystery for me. Better explained byt the following:
x = 0 ; print id(x) # an address
def f() : print x # 0
x = 1 ; print id(x) # another one
f() # 1
This shows, I guess, that the reference of the upvalue x is *not* an address. But the key (maybe the name itself ?) used by python to lookup a symbol's value, in a given scope, at runtime. Indeed f must find its upvalue in the global scope. Note the scope must also be referenced:
def f():
# not the global scope
x = 0
def g():
print x
x = 1
return g
# global scope
f()() # 1
I guess the above example also shows that upvalues can be "finalised", since here the scope is lost xwhen f runs.
Does anyone know if this reasoning is correct, and how this is done?
All of this mechanics looks very complicated. I would be happy with setting func attributes like x here as func attributes directly & explicitely:
def f():
def g():
print g.x
g.x = 1
... which are still modifiable --explicitely.
Denis
________________________________
vit esse estrany ☣
spir.wikidot.com
More information about the Tutor
mailing list