title: How to access private variable of Python module from class
body: <p>In Python 3, prefixing a class variable makes it private my mangling the name within the class. How do I access a module variable within a class?</p>
  
  <p>For example, the following two ways do not work:</p>
  
  <pre><code>__a = 3
  class B:
      def __init__(self):
          self.a = __a
  b = B()
  </code></pre>
  
  <p>results in:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 3, in __init__
  NameError: name '_B__a' is not defined
  </code></pre>
  
  <p>Using <code>global</code> does not help either:</p>
  
  <pre><code>__a = 3
  class B:
      def __init__(self):
          global __a
          self.a = __a
  b = B()
  </code></pre>
  
  <p>results in:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 4, in __init__
  NameError: name '_B__a' is not defined
  </code></pre>
  
  <p>Running <code>locals()</code> shows that the variable <code>__a</code> exists unmangled:</p>
  
  <pre><code>&gt;&gt;&gt; locals()
  {'__package__': None, '__name__': '__main__',
   '__loader__': &lt;class '_frozen_importlib.BuiltinImporter'&gt;,
   '__doc__': None, '__a': 3, 'B': &lt;class '__main__.B'&gt;,
   '__builtins__': &lt;module 'builtins' (built-in)&gt;, '__spec__': None}
  </code></pre>
  
  <p>[Newlines added for legibility]</p>
  
  <p>Running same code in a module (as opposed to interpreter) results in the exact same behavior. Using Anaconda's <code>Python 3.5.1 :: Continuum Analytics, Inc.</code>.</p>