Accept the function as an argument:
def rk4(diff, # accept an argument of the function to call
x, dt)
k1=diff(x)*dt
k2=diff(x+k1/2)*dt
k3=diff(x+k2/2)*dt
k4=diff(x+k3)*dt
return x+(k1+2*k2+2*k3+k4)/6
Then, when you call rk4, simply pass in the function to be executed:
from rk4 import rk4
import numpy as np
def diff(x):
return x
def mercury(u0,phi0,dphi):
x=np.array([u0,phi0])
dt=2
x=rk4(diff, # here we send the function to rk4
x, dt)
return x
mercury(1,1,2)
It might be a good idea for mercury to accept diff as an argument too, rather than getting it from the closure (the surrounding code). You then have to pass it in as usual - your call to mercury in the last line would read mercury(diff, 1, 1, 2).
Functions are 'first-class citizens' in Python (as is nearly everything, including classes and modules), in the sense that they can be used as arguments, be held in lists, be assigned to names in namespaces, etc etc.
Answer from Benjamin Hodgson on Stack OverflowJust a quick beginner question! How are functions executed in Python?
Python define function - Stack Overflow
Which is more pythonic? Should I have a main() function?
[Python] Why use a class instead of a basic function in the __main__ part of a script?
Videos
Accept the function as an argument:
def rk4(diff, # accept an argument of the function to call
x, dt)
k1=diff(x)*dt
k2=diff(x+k1/2)*dt
k3=diff(x+k2/2)*dt
k4=diff(x+k3)*dt
return x+(k1+2*k2+2*k3+k4)/6
Then, when you call rk4, simply pass in the function to be executed:
from rk4 import rk4
import numpy as np
def diff(x):
return x
def mercury(u0,phi0,dphi):
x=np.array([u0,phi0])
dt=2
x=rk4(diff, # here we send the function to rk4
x, dt)
return x
mercury(1,1,2)
It might be a good idea for mercury to accept diff as an argument too, rather than getting it from the closure (the surrounding code). You then have to pass it in as usual - your call to mercury in the last line would read mercury(diff, 1, 1, 2).
Functions are 'first-class citizens' in Python (as is nearly everything, including classes and modules), in the sense that they can be used as arguments, be held in lists, be assigned to names in namespaces, etc etc.
diff is already a global in the module mercury.py. But in order to use it in rk4.py you would need to import it like this:
from mercury import diff
That's the direct answer to your question.
However, passing the diff function to rk4 as suggested by @poorsod is much more elegant and also avoids a circular dependency between mercury.py and rk4.py, so I suggest you do it that way.
Hi, everyone! I've picked up Python 6 hours ago. My previous experience (more than 15 year ago) was limited to Turbo Pascal and a little C.
A normal program for me would look like:
define function
begin
call function
end
My Python program looks like:
define function1
call function2
define function2
call function 3
define function3
if cond1 call function1
elif cond2 do nothing
else call function 3
some other code
AND IT WORKS! The program does what I ask it to do without any errors. Is this normal? Does a python program start with the first function it encounters even if it's not called main() ?
You should simply move the function above the place that calls it, or put the loop in a function of its own. The following works fine, because the name is_number inside check_lines is not resolved until the function is called.
def check_lines(lines):
for check in lines:
if is_number(check):
print ("number: " + check)
else:
print ("String!" + check)
def is_number(s):
try:
float(s)
return True
except ValueError:
return False;
check_lines(lines)
In my Python scripts, I always define the functions at the top, then put a few lines of code calling them at the bottom. This convention makes it easy to follow the control flow, because it's not interspersed with definitions, and it also makes it easier to later reuse your script as a module, which is similar to a Java package: just look at the "script" code near the bottom and remove that to get an importable module. (Or protect it with an if __name__ == '__main__' guard.)
Actually, Python does act like C. The difference is, in C, before anything in module is executed, the entire file is compiled. In Python, it's run.
So, the following will work, just as it will in C:
def f(x):
return f2(x) + 1
def f2(x):
return x*2
The function definitions just load the code - they don't check anything in there for existence. So even though f2 doesn't exist yet when f is first seen, when you actually call f, f2 does exist in the module scope and it works fine.
This doesn't work:
print f2(x) + 1
def f2(x):
return x*2
In this case, when the module is loaded, it is being interpreted. So if it hits a print, it will execute it immediately. This can be used to your benefit, but it does cause a problem here, since we'll lookup f2 immediately and try to dereference it, and it won't exist yet.