title: Static variables in Python that are shared across files
body: <p>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:</p>
  
  <p><strong><em>data.py</em></strong></p>
  
  <pre><code>class Shared(object):
      list_vars = []
      var = ""
  
      @staticmethod
      def print_vars():
          print "list_vars = " + str(Shared.list_vars)
          print "var = " + Shared.var
  </code></pre>
  
  <p><strong><em>user1.py</em></strong></p>
  
  <pre><code>import time
  from data import Shared
  
  Shared.list_vars.append("001")
  time.sleep(5.0)
  Shared.print_vars()
  
  # It prints:
  # list_vars = ['001']
  # var =
  </code></pre>
  
  <p><strong><em>user2.py</em></strong></p>
  
  <pre><code>import time
  from data import Shared
  
  Shared.var = "002"
  time.sleep(5.0)
  Shared.print_vars()
  
  # It prints:
  # list_vars = []
  # var = 002
  </code></pre>
  
  <p>Sorry, this may not be the best example but it catches the idea... my question is, if I want to have:</p>
  
  <pre><code>list_vars = ['001']
  var = 002
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks a lot!</p>
  
  <p><strong>===============================</strong></p>
  
  <p><strong>Updates for the question:</strong></p>
  
  <p>To be a little more specific to the problem, please allow me to describe the problem in this way:</p>
  
  <p><strong><em>data.py</em></strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>There are two extra files, <strong><em>user1.py</em></strong> and <strong><em>user2.py</em></strong>, their content can be ignored for the moment...</p>
  
  <p>So let's say <strong><em>user1.py</em></strong> and <strong><em>user2.py</em></strong> are being run at about the same time, say time = 0.</p>
  
  <ul>
  <li><p>At time = 1, there is a message comes in (with message.event = "add"
  and message.data = "001"), the <strong><em>callback</em></strong> function in
  <strong><em>data.py</em></strong> is triggered and the <strong><em>"001"</em></strong> is added to the variable <strong><em>list_vars</em></strong>.</p></li>
  <li><p>At time = 2, <strong><em>user1.py</em></strong> updates the variable <strong><em>var</em></strong> in
  <strong><em>data.py</em></strong> to <strong><em>"002"</em></strong>.</p></li>
  <li><p>At time = 3, if <strong><em>user2.py</em></strong> wants to access and print out the
  variables <strong><em>list_vars</em></strong> and <strong><em>var</em></strong>, with the latest values:</p>
  
  <ul>
  <li>list_vars = ["001"]</li>
  <li>var = 002</li>
  </ul></li>
  </ul>
  
  <p>How can this be achieved in Python?</p>
  
  <p>Thank you very much!</p>