How do you partially apply a list of functions? The following minimal example:
def myprint(a,b):
print 'a: '+str(a)+', b: '+str(b)
l1=[lambda x:myprint('x',x), lambda y:myprint('y',y)]
l2=[]
for f in l1:
l2.append(lambda:f('!'))
for g in l2:
g()
gives this output:
a: y, b: ! a: y, b: !
(Naively) expected would be
a: x, b: ! a: y, b: !
However, lambda:f('!') is only executed in the last loop. Then f will remain its value from the first loop, i.e. the last element in l1. This last element is then used in the two different lambdas in l2.
My question: How do you make it work as expected?