0

I would like to define a function with one argument from a function with two arguments by fixing one of those. For example, let's say I have this function defined:

f(a,b):
return a*(b+1)

and I want to define a function g that does the same as f but with the first argument fixed to the number 3. It would be like doing

g = f(a=3)

but in a right way, such that for example g(2) returns 9 (3*(2+1)=9).

  • @Samwise Maybe this is beside the point, but [avoid named lambdas](/q/38381556/4518341). I'd use the `partial` instead, personally. – wjandrea Aug 26 '22 at 18:55
  • Other answers have resolved your immediate issue but you might want to look up partial in functools module for a generic approach. – user19077881 Aug 26 '22 at 18:58

1 Answers1

0

There are multiple ways to do this, but the easiest is just to define 2 functions like so:

def f(a, b):
  return a * (b+1)

def g(b):
  return f(3, b)
Kenny
  • 1
  • 1