I want to know how to pass values between modules in Python.
In generate_image.py I have:
def gerenate_image(fr,to,lat1,lon1,lat2,lon2):
output_image_name = spatial_matrix.plot_spatial_data(data_array,data_array.shape[0],data_array.shape[1],float(lon1)/10000,float(lon2)/10000,float(lat1)/10000,float(lat2)/10000,fr,to)
return()
In overlay.py, I want to use the "output_image_name",so I tried:
import generate_image
def overlay():
overlay = generate_image.output_image_name
....
but it didn't work. So how can I retrieve the value of output_image_name?Thanks.
-
2This is one of those situations where I recommend you read the Python tutorial from cover to cover.Martijn Pieters– Martijn Pieters2012年07月11日 08:38:04 +00:00Commented Jul 11, 2012 at 8:38
2 Answers 2
Make your function return something.
def generate_image(fr,to,lat1,lon1,lat2,lon2):
return spatial_matrix.plot_spatial_data(data_array,data_array.shape[0],data_array.shape[1],float(lon1)/10000,float(lon2)/10000,float(lat1)/10000,float(lat2)/10000,fr,to)
Then in the other place import and call the function.
from yourmodule import generate_image
def overlay():
background = generate_image(*args) # Or what ever arguments you want.
answered Jul 11, 2012 at 8:38
Jakob Bowyer
34.8k8 gold badges80 silver badges93 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
oops
Thank you for your answer! but the problem now is that the args in generate_image involves a lots of other modules and I have to import them all..is there an easier way to pass the value of "output_image_name" in gerenate_image.py?
In overlay.py:
def gerenate_image(fr,to,lat1,lon1,lat2,lon2):
return spatial_matrix.plot_spatial_data(...)
In generate_image.py:
import generate_image
def overlay():
overlay = generate_image.generate_image(...)
answered Jul 11, 2012 at 8:39
Otto Allmendinger
28.5k7 gold badges71 silver badges79 bronze badges
4 Comments
Jakob Bowyer
Yes that and on
overlay = generate_image.generate_image() where it expects 6 arguments and you give it none.Otto Allmendinger
That wouldn't be a syntax error. I've shortened the code to get the main point across.
Jakob Bowyer
But people that didn't know that would try to run it, get a syntax error
Otto Allmendinger
Calling a function with the wrong number of arguments is a
TypeErrorlang-py