1

Given a function fxn1()

def fxn1(a,b=1):
    return a*b

I would like to do something like:

fxn2=fxn1(.,b=2)

In words, I would like to make a new function fxn2() which is identical to fxn1() but with different default values of optional arguments. I have not been able to find an answer/direction on web.

user3342981
  • 85
  • 2
  • 7

4 Answers4

2

You can use partial function:

from functools import partial
fxn2 = partial(fxn1, b=2)

fxn2(1)
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34
2

You can use functools.partial (official doc):

from functools import partial

def fxn1(a,b=1):
    return a*b

fxn2 = partial(fxn1, b=2)

print(fxn2(4))

Prints:

8
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1
def fxn1(a,b=1):
    return a*b

def fxn2(a,b=2):
    return fxn1(a,b)
0

The easiest way will be

def fxn2(a, b=2):
    return fxn1(a, b)
Arnav Poddar
  • 354
  • 2
  • 18
JohnO
  • 777
  • 6
  • 10