簡體   English   中英

從python中的另一個文件訪問函數變量

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

我有兩個文件File1File2 File1我有一個函數detect()定義為:

def detect():
    s = // some operations

我需要訪問這個變量sFile2

我嘗試將變量聲明為global變量,但沒有用。

我怎樣才能訪問變量,而無需創建一個類,在這個崗位或使用__main__在這個崗位

必須運行功能檢測以初始化其局部變量。

def detect():
    detect.tmp = 1

def b():
    print(detect.tmp)

detect()
b()

當然,您可以將一個python文件作為python模塊導入並調用其功能,例如

from File1 import detect
print(detect.tmp)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM