Static variables in Python that are shared across

2020-04-14 08:13发布

问题:

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

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?

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

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

So let's say user1.py and user2.py are being run at about the same time, say 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.

  • At time = 2, user1.py updates the variable var in data.py to "002".

  • At time = 3, if user2.py wants to access and print out the variables list_vars and var, with the latest values:

    • list_vars = ["001"]
    • var = 002

How can this be achieved in Python?

Thank you very much!

回答1:

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.

But Python is not Java, and everything does not need to be a class. You could do :

  • data.py

    list_vars = []
    var = ""
    
    def print_vars():
        print "list_vars = " + str(Shared.list_vars)
        print "var = " + Shared.var
    
  • 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

    import time
    import data as shared
    
    shared.var = "002"
    time.sleep(5.0)
    shared.print_vars()
    
    # It prints:
    # list_vars = []
    # var = 002
    
  • main.py

    import user1
    # It prints:
    # list_vars = ['001']
    # var =
    import user2
    # It prints:
    # list_vars = ['001']
    # var = 002