问题描述
我对 Python 很陌生,我找不到一种方法来让静态变量在不同的 Python 文件之间共享......在 Java 中,这样做似乎很简单......这就是我我正在尝试:
I am pretty new to Python and I could't find a way to have static variables that are shared across different python files... In Java, it seems quite straight forward to do so... Here is what I'm trying to do:
data.py
class Shared(object):
list_vars = []
var = ""
@staticmethod
def print_vars():
print "list_vars = " + str(Shared.list_vars)
print "var = " + Shared.var
user1.py
import time
from data import Shared
Shared.list_vars.append("001")
time.sleep(5.0)
Shared.print_vars()
# It prints:
# list_vars = ['001']
# var =
user2.py
import time
from data import Shared
Shared.var = "002"
time.sleep(5.0)
Shared.print_vars()
# It prints:
# list_vars = []
# var = 002
对不起,这可能不是最好的例子,但它抓住了这个想法......我的问题是,如果我想要:
Sorry, this may not be the best example but it catches the idea... my question is, if I want to have:
list_vars = ['001']
var = 002
在运行 user1.py 和 user2.py 后,在其中一个/甚至一些其他 python 文件或类中(我将 user1.py 和 user2.py 视为同一软件包中的两个子程序,它们通常被同时运行),这样做的Python方式"是什么?
in one of the/even some other python files or classes after running both user1.py and user2.py (I am considering user1.py and user2.py as two sub-programs in the same software package that are normally being run at the same time), what is the "Python way" to do so?
非常感谢!
================================
问题更新:
为了更具体一点,请允许我这样描述问题:
To be a little more specific to the problem, please allow me to describe the problem in this way:
data.py
class Shared(object):
list_vars = []
var = ""
@staticmethod
def print_vars():
print "list_vars = " + str(Shared.list_vars)
print "var = " + Shared.var
# Can be considered as a separate thread running in the background, waiting for messages
@staticmethod
def callback(message):
if message.event == "add":
Shared.list_vars.append(message.data)
elif message.event == "remove" and message.data in Shared.list_vars:
Shared.list_vars.remove(message.data)
有两个额外的文件,user1.py 和 user2.py,它们的内容可以忽略暂时...
There are two extra files, user1.py and user2.py, their content can be ignored for the moment...
假设 user1.py 和 user2.py 几乎同时运行,说时间 = 0.
So let's say user1.py and user2.py are being run at about the same time, say time = 0.
在时间 = 1 时,有消息传入(message.event = "add"和 message.data = "001"),回调 函数在data.py 被触发并且 "001" 被添加到变量 list_vars.
在时间 = 2 时,user1.py 更新变量 vardata.py 到 002".
At time = 2, user1.py updates the variable var indata.py to "002".
在时间 = 3 时,如果 user2.py 想要访问并打印出变量 list_vars 和 var,具有最新值:
At time = 3, if user2.py wants to access and print out thevariables list_vars and var, with the latest values:
- list_vars = ["001"]
- var = 002
如何在 Python 中实现这一点?
How can this be achieved in Python?
非常感谢!
推荐答案
问题是(我想)你是分开启动 user1.py
和 user2.py
.所以 Shared
类的静态值不会在不同的调用中保留.
The problem is that (I suppose) you are launching separately user1.py
and user2.py
. So static values of class Shared
are not preserved across different invocations.
但这是一种完全正确的数据共享方式:您在一个文件中声明类甚至变量,然后在其他文件中导入.
But that's otherwise a perfectly correct way of sharing data : you declare classes or even variable in one file that is imported in the others.
但是 Python 不是 Java,一切都不需要是一个类.你可以这样做:
But Python is not Java, and everything does not need to be a class. You could do :
data.py
data.py
list_vars = []
var = ""
def print_vars():
print "list_vars = " + str(Shared.list_vars)
print "var = " + Shared.var
user1.py
user1.py
import time
import data as shared
shared.list_vars.append("001")
time.sleep(5.0)
shared.print_vars()
# It prints:
# list_vars = ['001']
# var =
user2.py
user2.py
import time
import data as shared
shared.var = "002"
time.sleep(5.0)
shared.print_vars()
# It prints:
# list_vars = []
# var = 002
main.py
main.py
import user1
# It prints:
# list_vars = ['001']
# var =
import user2
# It prints:
# list_vars = ['001']
# var = 002
这篇关于Python 中跨文件共享的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!