0

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.

1
  • 2
    This is one of those situations where I recommend you read the Python tutorial from cover to cover. Commented Jul 11, 2012 at 8:38

2 Answers 2

4

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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?
1

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(...)

4 Comments

Yes that and on overlay = generate_image.generate_image() where it expects 6 arguments and you give it none.
That wouldn't be a syntax error. I've shortened the code to get the main point across.
But people that didn't know that would try to run it, get a syntax error
Calling a function with the wrong number of arguments is a TypeError

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.