Hello all,
To me, this is a somewhat unintuitive behavior. I want to discuss the
parts of it I don't understand.
.... f[ n ]= lambda: n
....
9
9
I guess I can accept this part so far, though it took a little getting
used to. I'm writing some code and found the following workaround,
but I don't think it should give different results. Maybe I'm not
understanding some of the details of closures.
.... f[ n ]= (lambda n: ( lambda: n ) )( n )
....
0
1
Which is of course the desired effect. Why doesn't the second one
just look up what 'n' is when I call f[0], and return 9?
To me, this is a somewhat unintuitive behavior. I want to discuss the
parts of it I don't understand.
>>f= [ None ]* 10
>>for n in range( 10 ):
>>for n in range( 10 ):
....
>>f[0]()
>>f[1]()
I guess I can accept this part so far, though it took a little getting
used to. I'm writing some code and found the following workaround,
but I don't think it should give different results. Maybe I'm not
understanding some of the details of closures.
>>f= [ None ]* 10
>>for n in range( 10 ):
>>for n in range( 10 ):
....
>>f[0]()
>>f[1]()
Which is of course the desired effect. Why doesn't the second one
just look up what 'n' is when I call f[0], and return 9?
Comment