Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Python, is it possible to redefine the default parameters of a function at runtime?

I defined a function with 3 parameters here:

def multiplyNumbers(x,y,z):
    return x*y*z

print(multiplyNumbers(x=2,y=3,z=3))

Next, I tried (unsuccessfully) to set the default parameter value for y, and then I tried calling the function without the parameter y:

multiplyNumbers.y = 2;
print(multiplyNumbers(x=3, z=3))

But the following error was produced, since the default value of y was not set correctly:

TypeError: multiplyNumbers() missing 1 required positional argument: 'y'

Is it possible to redefine the default parameters of a function at runtime, as I'm attempting to do here?

like image 770
Anderson Green Avatar asked Jul 13 '13 00:07

Anderson Green


People also ask

How do I change the default value of a variable in Python?

Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.

What is a default parameter in Python?

Default arguments in Python functions are those arguments that take default values if no explicit values are passed to these arguments from the function call.

Does Python function change parameter value?

Functions in Python also assigns parameter value to a local variable, but the assignment looks much different. Instead of storing a copy of the parameter value in the local variable, Python simply has the local variable refer to the value.

Can all the parameters of a function can be default parameters?

C. All the parameters of a function can be default parameters.


2 Answers

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.
    
like image 177
RiaD Avatar answered Sep 22 '22 08:09

RiaD


Technically, it is possible to do what you ask… but it's not a good idea. RiaD's answer is the Pythonic way to do this.

In Python 3:

>>> def f(x=1, y=2, z=3):
...     print(x, y, z)
>>> f()
1 2 3
>>> f.__defaults__ = (4, 5, 6)
4 5 6

As with everything else that's under the covers and hard to find in the docs, the inspect module chart is the best place to look for function attributes.

The details are slightly different in Python 2, but the idea is the same. (Just change the pulldown at the top left of the docs page from 3.3 to 2.7.)


If you're wondering how Python knows which defaults go with which arguments when it's just got a tuple… it just counts backward from the end (or the first of *, *args, **kwargs—anything after that goes into the __kwdefaults__ dict instead). f.__defaults = (4, 5) will set the defaults to y and z to 4 and 5, and with default for x. That works because you can't have non-defaulted parameters after defaulted parameters.


There are some cases where this won't work, but even then, you can immutably copy it to a new function with different defaults:

>>> f2 = types.FunctionType(f.__code__, f.__globals__, f.__name__,
...                         (4, 5, 6), f.__closure__)

Here, the types module documentation doesn't really explain anything, but help(types.FunctionType) in the interactive interpreter shows the params you need.


The only case you can't handle is a builtin function. But they generally don't have actual defaults anyway; instead, they fake something similar in the C API.

like image 37
abarnert Avatar answered Sep 19 '22 08:09

abarnert