Question about lambda and variable bindings

Michael Torrie torriem at gmail.com
Sat Mar 1 22:20:32 EST 2008


poof65 wrote:
> 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))

Good idea.  I will try it.  I also figured out a way to architecture my
program differently to avoid this problem.  But this idiom might be
handy in certain situations.  Suppose you need to provide a callback
function with a fixed signature to some library routine.  But you want
to add extra contextual data to your callback.  This just might be the
ticket.

Michael



More information about the Python-list mailing list