我做了两个节目。我想将全局变量从transmitter函数导入到另一个文件中,但是我遇到的问题是While true循环也会破坏我的整个第二个程序,因为第二个问题现在也开始显示它
方案1:
import time
def transmitter():
global temp
global temp
temp = 2
transmitter()
while True: # a random task just to see if I only imported the function
x = 0
print(x + 1)
time.sleep(0.2)
方案2:
from transmitguy import transmitter
def valuepullup():
newval = transmitguy.transmitter()
print(newval)
valuepullup()
我只需要我的第二个程序显示一次2的值(2是文件1中的globalvar)
简而言之,你不可能只得到一个模块的一部分} 说:
from x import y
以import x
相同的方式导入x
。唯一的区别是,在前一种情况下,y
被添加到当前全局名称空间,在后一种情况下,x
被添加到当前全局名称空间。这个docs for ^{我不确定你到底想做什么。正如评论者所指出的,您可以检查
__main__
。但是,最好将变量放在自己的模块中,然后从两个现有模块导入该模块另见the tutorial
相关问题 更多 >
编程相关推荐