[Tutor] return integer from function

Steven D'Aprano steve at pearwood.info
Wed Feb 22 15:18:37 CET 2012

David Craig wrote:
> Hi,
> I have a function that calculates the distance between two points on a 
> sphere. It works but I cant get it to return a float for use in another 
> script. Anyone know how I do that??

You are going to have to explain what you mean in more detail.

If you mean, how can you pass the output of one script as the input of another 
in the shell, remember that the shell communicates by passing strings through 
stdout and stdin. So you need to have your first script actually print its 
output, and then pipe it to the next script, just like you would when using 
shell commands.

On the other hand, if you mean, how do you re-use your distance_on_unit_sphere 
function in another Python program, you need to import it exactly the same as 
you import math. For example, if your distance function is in a file 
"sphere.py", and you want to use it in a file "calculations.py" (say), then 
put this line at the top of calculations.py:

import sphere

and now you can use sphere.distance_on_unit_sphere just as you can use 
math.sin and math.cos.

For this to work, though, sphere.py and calculations.py need to be in the same 
folder. (There are other ways to make that work, but they start getting a 
little tricky to get right.)

On the third hand, if you mean something else, you're going to need to explain 
what you mean because I have no idea what it might be! :)


More information about the Tutor mailing list