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
  • 1
  • 6

4 Answers4

2

You can use partial function:

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

fxn2(1)
Nikita Ryanov
  • 1,370
  • 2
  • 15
  • 28
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
  • 118,151
  • 13
  • 38
  • 75
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
  • 336
  • 2
  • 17
JohnO
  • 757
  • 5
  • 10