简体   繁体   English

在Python中的模块之间传递值

[英]Passing values between modules in Python

I want to know how to pass values between modules in Python. 我想知道如何在Python模块之间传递值。

In generate_image.py I have: 在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()

In overlay.py, I want to use the "output_image_name",so I tried: 在overlay.py中,我想使用“ output_image_name”,所以我尝试了:

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. 那么,如何获取output_image_name的值呢?

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.

In overlay.py : overlay.py

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

In generate_image.py : generate_image.py

import generate_image

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM