0

我想知道如何在 Python 中的模块之间传递值。

在 generate_image.py 我有:

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

在overlay.py中,我想使用“output_image_name”,所以我尝试了:

import generate_image

def overlay():
    overlay = generate_image.output_image_name
    ....

但它没有用。那么如何检索 output_image_name 的值呢?谢谢。

4

2 回答 2

4

让你的函数返回一些东西。

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)

然后在另一个地方导入并调用该函数。

from yourmodule import generate_image

def overlay():
    background = generate_image(*args) # Or what ever arguments you want.
于 2012-07-11T08:38:24.530 回答
1

overlay.py

def gerenate_image(fr,to,lat1,lon1,lat2,lon2):
    return spatial_matrix.plot_spatial_data(...)

generate_image.py

import generate_image

def overlay():
    overlay = generate_image.generate_image(...)
于 2012-07-11T08:39:02.020 回答