Can one partially apply the second argument of a function that takes no keyword arguments?

前端 未结 10 1633
太阳男子
太阳男子 2020-11-28 06:45

Take for example the python built in pow() function.

xs = [1,2,3,4,5,6,7,8]

from functools import partial

list(map(partial(pow,2),xs))

>&g         


        
10条回答
  •  感情败类
    2020-11-28 07:45

    you could use a closure

    xs = [1,2,3,4,5,6,7,8]
    
    def closure(method, param):
      def t(x):
        return method(x, param)
      return t
    
    f = closure(pow, 2)
    f(10)
    f = closure(pow, 3)
    f(10)
    

提交回复
热议问题