Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
Re: Question about lambda and variable bindings
poof65
3/2/2008 3:11:00 AM
An idea, i don't know if it will work in your case.
for x in xrange(10):
funcs.append(lambda p,z=x: testfunc(z+2,p))
On Sun, Mar 2, 2008 at 3:50 AM, Michael Torrie <torriem@gmail.com> wrote:
> I need to use a lambda expression to bind some extra contextual data
> (should be constant after it's computed) to a call to a function. I had
> originally thought I could use something like this demo (but useless) code:
>
> funcs=[]
>
> def testfunc(a,b):
> print "%d, %d" % (a,b)
>
> for x in xrange(10):
> funcs.append(lambda p: testfunc(x+2,p))
>
>
> Now what I'd like is to call, for example, funcs[0](4) and it should
> print out "2,4". In other words I'd like the value of x+2 be encoded
> into the lambda somehow, for funcs[x]. However the disassembly shows
> this, which is reasonable, but not what I need:
>
> >>> dis.dis(funcs[0])
> 2 0 LOAD_GLOBAL 0 (testfunc)
> 3 LOAD_GLOBAL 1 (x)
> 6 LOAD_CONST 0 (2)
> 9 BINARY_ADD
> 10 LOAD_FAST 0 (p)
> 13 CALL_FUNCTION 2
> 16 RETURN_VALUE
>
> The LOAD_GLOBAL 1 (x) line is definitely a problem. For one it refers
> to a variable that won't be in scope, should this lambda be called from
> some stack frame not descended from the one where I defined it.
>
> So how can I create a lambda expression that calculates a constant based
> on an expression, rather than referring to the object itself? Can it be
> done?
>
> Michael
> --
>
http://mail.python.org/mailman/listinfo/p...
>
3 Answers
RBRK
11/30/2008 7:19:00 AM
0
RBRK wrote:
> CNN - Helping child brides break free - "When I got married I was
> scared," remembers 10-year-old Nujood Ali. "I didn't want to leave my
> family and siblings." - At an age when girls in the West still play
> with dolls, Nujood found herself married to a man three times her age.
> But in her home country of Yemen -- a deeply conservative Middle East
> Muslim nation -- this situation isn't uncommon.,,Yemeni lawyer Shada
> Nasser had long opposed the practice of early marriage when, in April
> 2008, she got a chance to do something about it. -
>
http://edition.cnn.com/2008/WORLD/meast/09/26/heroes.shada.nasser/...
>
RBRK
12/2/2008 7:30:00 AM
0
RBRK wrote:
> RBRK wrote:
>> CNN - Helping child brides break free - "When I got married I was
>> scared," remembers 10-year-old Nujood Ali. "I didn't want to leave my
>> family and siblings." - At an age when girls in the West still play
>> with dolls, Nujood found herself married to a man three times her age.
>> But in her home country of Yemen -- a deeply conservative Middle East
>> Muslim nation -- this situation isn't uncommon.,,Yemeni lawyer Shada
>> Nasser had long opposed the practice of early marriage when, in April
>> 2008, she got a chance to do something about it. -
>>
http://edition.cnn.com/2008/WORLD/meast/09/26/heroes.shada.nasser/...
>>
RBRK
12/4/2008 6:11:00 AM
0
RBRK wrote:
> CNN - Helping child brides break free - "When I got married I was
> scared," remembers 10-year-old Nujood Ali. "I didn't want to leave my
> family and siblings." - At an age when girls in the West still play
> with dolls, Nujood found herself married to a man three times her age.
> But in her home country of Yemen -- a deeply conservative Middle East
> Muslim nation -- this situation isn't uncommon.,,Yemeni lawyer Shada
> Nasser had long opposed the practice of early marriage when, in April
> 2008, she got a chance to do something about it. -
>
http://edition.cnn.com/2008/WORLD/meast/09/26/heroes.shada.nasser/...
>
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Question about lambda and variable bindings
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password