title: Python 3, Global Variables, Modules
body: <p>how can I move this code to a function in a module?
  I have global var 'last_msg' and 'fake'. I was trying use 'global' for 'last_msg' in my function, but it out of scope because function in a module, but 'last_msg' in main scope.</p>
  
  <pre><code>main.py
  
  from module import Timeout
  
  last_msg = {'Foo': 0}
  name = 'Foo'
  fake = False
  timeout = 3
  
  fake = Timeout(fake, name, timeout)
  
  &gt;&gt; NameError: name 'last_msg' is not defined
  </code></pre>
  
  <p>&lt;></p>
  
  <pre><code>module.py
  
  def Timeout(fake, name, timeout):
      global last_msg
  
      if not fake:
          if name not in last_msg:
              last_msg[name] = 0
  
          if last_msg[name] &gt; 0:
              last_msg[name] -= 1
              fake = True
          else:
              last_msg[name] = timeout
      else:
          if name in last_msg:
              last_msg[name] = 0
  
      return fake
  </code></pre>