设置参数默认来自python中的参数

buz*_*zsh 5 python arguments default python-2.7

我正在尝试为我定义的函数中的参数设置默认值.我还希望另一个参数的默认值取决于另一个参数.在我的例子中,我试图绘制氢的量子力学波函数,但你不需要知道物理学来帮助我.

def plot_psi(n,l,start=(0.001*bohr),stop=(20*bohr),step=(0.005*bohr)):
Run Code Online (Sandbox Code Playgroud)

n主要量子数在哪里,l是角动量,start,stop,step将是我计算的数组.但我需要的是,stop实际的默认值取决于n,这n将影响波函数的大小.

def plot_psi(n,l,start=(0.001*bohr),stop=((30*n-10)*bohr),step=(0.005*bohr)):
Run Code Online (Sandbox Code Playgroud)

将是我想要的,但n尚未定义因为线路不完整.有解决方案吗 或者想法以另一种方式来安排它?谢谢

the*_*eye 3

用作None默认值,并计算函数内的值,如下所示

def plot_psi(n, l, start=(0.001*bohr),stop=None,step=(0.005*bohr)):
    if stop is None:
        stop = ((30*n-10)*bohr)
Run Code Online (Sandbox Code Playgroud)