Is it possible to change a function’s default parameters in Python?

Just use functools.partial

 multiplyNumbers = functools.partial(multiplyNumbers, y = 42)

One problem here: you will not be able to call it as multiplyNumbers(5, 7, 9); you should manually say y=7

If you need to remove default arguments I see two ways:

  1. Store original function somewhere

    oldF = f
    f = functools.partial(f, y = 42)
    //work with changed f
    f = oldF //restore
    
  2. use partial.func

    f = f.func //go to previous version.
    

Leave a Comment