简体   繁体   English

从python中的另一个文件访问函数变量

[英]Accessing a function variable from another file in python

I have two files File1 and File2 . 我有两个文件File1File2 In File1 I have a function detect() defined as: File1我有一个函数detect()定义为:

def detect():
    s = // some operations

I need to access this variable s in File2 . 我需要访问这个变量sFile2

I have tried declaring the variable as global but it has not worked. 我尝试将变量声明为global变量,但没有用。

How can I access the variable without creating a class as in this post or by using __main__ as in this post ?? 我怎样才能访问变量,而无需创建一个类,在这个岗位或使用__main__在这个岗位

function detect must be run to init its local variables. 必须运行功能检测以初始化其局部变量。

def detect():
    detect.tmp = 1

def b():
    print(detect.tmp)

detect()
b()

of course you can import one python file as python module and call its functions like 当然,您可以将一个python文件作为python模块导入并调用其功能,例如

from File1 import detect
print(detect.tmp)

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

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