title: Python hook for module initialization
body: <p>I have a python <code>module_A</code> which is doing something like this:</p>
  
  <pre><code>myvar = "Hello"
  
  def message():
      print myvar
  </code></pre>
  
  <p>From another module, I want to import the function <code>message</code>:</p>
  
  <pre><code>from module_A import message
  </code></pre>
  
  <p>But this is defining <code>myvar</code>, which I do not want (for reasons not really relevant here). I guess what I need is to make a distinction between <em>importing</em> things from a module, and <em>using</em> any of the imported symbols. I actually want <code>myvar</code> to be initialized <em>only</em> when I use <code>message</code>.</p>
  
  <p>I there any kind of python hook that I could use to initialize stuff whenever a function or class is accessed? I would like something similar to the following:</p>
  
  <pre><code>module_setup_done = False
  def setup():
      global module_setup_done, myvar
      if not module_setup_done:
          myvar = "Hello"
          module_setup_done = True
  
  def message():
      setup()
      print myvar
  </code></pre>
  
  <p>But, to reduce clutter, I would like this to be called automatically, something like this:</p>
  
  <pre><code>def _init_module():
      global myvar
      myvar = "Hello"
  
  def message():
      print myvar
  </code></pre>
  
  <p>Where <code>_init_module()</code> would be called only <em>once</em>, and only the first time that something in the module is <em>accessed</em>, not when something is <em>imported</em>.</p>
  
  <p>Is there support for this kind of pattern in python?</p>