Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting argument defaults from arguments in python

I'm trying to set a default value for an argument in a function I've defined. I also want another argument to have a default value dependent on the other argument. In my example, I'm trying to plot the quantum mechanical wavefunction for Hydrogen, but you don't need to know the physics to help me.

def plot_psi(n,l,start=(0.001*bohr),stop=(20*bohr),step=(0.005*bohr)):

where n is the principle quantum number, l is the angular momentum and start,stop,step will be the array I calculate over. But what I need is that the default value of stop actually depends on n, as n will effect the size of the wavefunction.

def plot_psi(n,l,start=(0.001*bohr),stop=((30*n-10)*bohr),step=(0.005*bohr)):

would be what I was going for, but n isn't yet defined because the line isn't complete. Any solutions? Or ideas for another way to arrange it? Thanks

like image 233
buzsh Avatar asked Oct 19 '22 20:10

buzsh


1 Answers

Use None as the default value, and calculate the values inside the function, like this

def plot_psi(n, l, start=(0.001*bohr),stop=None,step=(0.005*bohr)):
    if stop is None:
        stop = ((30*n-10)*bohr)
like image 77
thefourtheye Avatar answered Oct 22 '22 23:10

thefourtheye