简体   繁体   English

跨文件共享的Python中的静态变量

[英]Static variables in Python that are shared across files

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: 我对Python很陌生,我找不到在不同python文件之间共享静态变量的方法...在Java中,这样做似乎很简单...这就是我要尝试的方法做:

data.py 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 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 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

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? 在同时运行user1.py和user2.py之后(甚至是其他一些python文件或类中)(我将user1.py和user2.py视为同一软件包中的两个子程序,通常在同时),这样做的“ Python方式”是什么?

Thanks a lot! 非常感谢!

=============================== ==============================

Updates for the question: 问题的更新:

To be a little more specific to the problem, please allow me to describe the problem in this way: 为了更具体地说明问题,请允许我以这种方式描述问题:

data.py 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)

There are two extra files, user1.py and user2.py , their content can be ignored for the moment... 有两个额外的文件, user1.pyuser2.py ,它们的内容暂时可以忽略...

So let's say user1.py and user2.py are being run at about the same time, say time = 0. 因此,假设user1.pyuser2.py大约在同一时间运行,例如time = 0。

  • At time = 1, there is a message comes in (with message.event = "add" and message.data = "001"), the callback function in data.py is triggered and the "001" is added to the variable list_vars . 在时间= 1时,出现一条消息(message.event =“ add”和message.data =“ 001”),触发了data.py中回调函数, 并将 “ 001”添加到变量list_vars

  • At time = 2, user1.py updates the variable var in data.py to "002" . 在时间= 2,user1.py更新data.py变量var“002”。

  • At time = 3, if user2.py wants to access and print out the variables list_vars and var , with the latest values: 在时间= 3时,如果user2.py要访问并打印具有最新值的变量list_varsvar

    • list_vars = ["001"] list_vars = [“ 001”]
    • var = 002 变量= 002

How can this be achieved in Python? 如何在Python中实现?

Thank you very much! 非常感谢你!

The problem is that (I suppose) you are launching separately user1.py and user2.py . 问题是(我想)您正在分别启动user1.pyuser2.py So static values of class Shared are not preserved across different invocations. 因此, Shared类的静态值不会在不同的调用之间保留。

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. 但这是共享数据的一种完全正确的方式:您可以在一个文件中声明类,甚至在另一个文件中导入变量。

But Python is not Java, and everything does not need to be a class. 但是Python不是Java,并且所有内容都不必是一个类。 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 

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

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