Python - 传递作为函数其他参数函数的参数的默认值

bra*_*boy 4 python arguments function

我想编写一个函数,其中一个参数的默认值是一些传递给同一函数的参数的函数.像这样的东西:

def function(x, y = function2(x)):
    ##definition of the function
Run Code Online (Sandbox Code Playgroud)

是否可以在Python中编写这样的函数?我遇到了这个 c ++的答案.但是Python中没有方法重载

提前致谢.

ded*_*bed 7

解决这个问题的一种常用方法是使用None占位符:

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

    pass  # whatever function() needs to do
Run Code Online (Sandbox Code Playgroud)