Evaluating a list of python lambda functions only evaluates the last list element

后端 未结 3 967
旧时难觅i
旧时难觅i 2020-12-17 16:45

I have a list of lambda functions I want to evaluate in order. I\'m not sure why, but only the last function gets evaluated. Example below:

  >>> de         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 17:05

    try using partial, works for me:

    from functools import partial
    
    def f(x,z):
        print "x=",x,", z=",z
    
    lst = [ partial(f,z=i) for i in range(5) ]
    
    for fn in lst:
        fn(3)
    

    http://docs.python.org/library/functools.html#functools.partial

提交回复
热议问题