Pass a function as a variable with one input fixed

前端 未结 4 1599
庸人自扰
庸人自扰 2020-12-19 12:16

Say I have a two dimensional function f(x,y) and another function G(function) that takes a function as an input. BUT, G only takes one dimensional functions as input and I\

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 12:59

    Try this:

    def h():
        return lambda x: f(x,c)
    

    No need to supply the x to h - you could pass a function to wrap eventually if it wouldn't be in the scope. In fact, h is obsolete unless you want it to work with any 2-argument function:

    def h(functionToWrap, fixedSecondArgument):
         return lambda x: functionToWrap(x, fixedSecondArgument)
    

提交回复
热议问题