简体   繁体   中英

Accessing a function variable from another file in python

I have two files File1 and File2 . In File1 I have a function detect() defined as:

def detect():
    s = // some operations

I need to access this variable s in File2 .

I have tried declaring the variable as global but it has not worked.

How can I access the variable without creating a class as in this post or by using __main__ as in this post ??

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

from File1 import detect
print(detect.tmp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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