我的代码中有一个导入的模块。该模块包含一个定义的功能。该函数不返回任何值。在我的代码中,我想访问在该函数中声明的变量。有办法吗?
修改 因此,OSS内部有一个生成语音序列的模块。但是语音序列变量不是全局的,函数也不返回语音序列。我想在我的插件中访问语音序列。
答案 0 :(得分:0)
If your function looks like this:
def function():
...
my_variable = "foo"
Then you can add a parameter that would force that variable to be returned along with anything that is being returned. So the function would look like this.
def function(get_variable=False):
...
return_variable = "bar"
my_variable = "foo"
if get_variable:
return (my_variable, return_variable)
else:
return return_variable
And then access it like this:
import module
my_variable, return_variable = module.function(get_variable=True)
You don't need this sort of hack if your function doesn't return anything at all. Just make it return the variable you want and assign it to something inside your main code's scope.