Python:定义一个用“from module import *”导入的函数中调用的变量

时间:2017-06-08 09:32:18

标签: python python-3.x

我正在尝试在需要首先定义变量的模块中定义一个函数:

def function(string):
    variable.write(string)

应该在包含以下内容的脚本中调用此函数:

variable = open(filepath, 'w')
...
some script
...
variable.close()

如果我执行以下操作,一切正常:

import mymodule.myfunctions
mymodule.myfunctions.variable = open(filepath, 'w')
...
some script calling function(string)
...
mymodule.myfunctions.variable.close()

但是如果我想使用“import *”它不起作用:

from mymodule.myfunctions import * 
function.variable = open(filepath, 'w') #does not return an error
function(string) #Returns an error : 'variable' is not defined

所以我希望能够使用'import mymodule.myfunctions'来定义变量,但是在使用'import *'时...你有什么建议吗?

谢谢!

0 个答案:

没有答案
相关问题