python - 从 Python 循环中的另一个文件访问变量
问题描述
我有一个主要读取测量值的文件。这个函数在一个 while True 循环中。在这个循环中,我想在变量通过过程时更改它。设置它没有问题。我遇到的问题是从另一个文件访问这个变量。
文件 1:
def main()
print("obtaining token")
obtainnewtoken()
while True:
print("******LOOP****** + str(i)")
(read measurement stuff )
postTrue = True
return postTrue
文件 2:
from File1 import *
newPostTrue = main()
def codechecker():
print(newPostTrue)
当我同时运行这两个文件时,File2 只运行 File1 的主文件。如何访问另一个文件中循环中的变量?
另外我仍然想分别运行这两个文件。此设置是临时的。
解决方案
您可以使用称为生成器的东西,它会“生成”一个值一次,然后您可以使用 next() 函数从生成器中获取下一个值。
文件_1:
def Generator():
i = 0
while True:
print("******LOOP******" + str(i))
i += 1
yield i
文件_2:
from File_1 import *
newPostTrue = Generator()
def codechecker():
j = next(newPostTrue)
while (j < 10):
print(j)
j = next(newPostTrue)
codechecker()
推荐阅读
- java - 在 Java 中对导入进行分组
- excel - 如何以原始语言在 Excel 单元格中显示解码错误的 Unicode 字符?
- git - git lfs 迁移和从上游拉取
- java - 试图在公共静态空白中繁殖。任何帮助表示赞赏
- google-cloud-platform - 将镜像从 Docker 注册表移动到 GCR
- sql-server - 连接 SQL Server 中“Value1 - Value9”列中的值?
- openstack - openstack新手:即使密码正确,为什么还要给我这个错误?
- google-kubernetes-engine - 使用 GKE Ingress 限制每个 pod 一个连接
- flutter - 在 Flutter 应用程序中处理应用程序布局
- python - 基于约束的数据框列生成