title: Unbound variable in python
body: <p>I am trying to make a toy singleton in python to learn the ins and outs of the language and am running into a problem with how python works.  I declare the class like this</p>
  
  <pre><code>class ErrorLogger:
    # Singleton that provides logging to a file  
    instance = None
  
    def getInstance():
      # Our singleton "constructor"
      if instance is None :
        print "foo"
  </code></pre>
  
  <p>when I call it with</p>
  
  <pre><code>log = ErrorLogger.getInstance()
  </code></pre>
  
  <p>I get </p>
  
  <pre><code> File "/home/paul/projects/peachpit/src/ErrorLogger.py", line 7, in getInstance
      if instance is None :
   UnboundLocalError: local variable 'instance' referenced before assignment
  </code></pre>
  
  <p>What is going on here, shouldn't instance be statically assigned Null?  What would be the right way to do this?</p>