Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Pass default values for arguments that are function of other arguments of the function

I would like to write a function in which the default value of one argument is a function of some arguments that are being passed to the same function. Something like this:

def function(x, y = function2(x)):
    ##definition of the function

Is it possible to write such a function in Python? I came across this answer for c++. But there is no method overloading in Python

Thanks in advance.

like image 918
braceletboy Avatar asked Jan 28 '23 21:01

braceletboy


1 Answers

A pretty usual way of solving this is by using None as a placeholder:

def function(x, y=None):
    if y is None:
        y = f2(x)

    pass  # whatever function() needs to do
like image 198
dedObed Avatar answered Jan 30 '23 10:01

dedObed