title: Python - Why is this class variable not defined in the method?
body: <p>I have a python application which is as follows:</p>
  
  <pre><code>global_counter = 0
  connections = {}
  
  class SocketHandler():
      currentid = 0
      def open(self):
          global global_counter
          global connections
          currentid = global_counter
          global_counter += 1
          connections[currentid] = self
          print "WebSocket " + str(currentid) + " opened"
  
      def on_close(self):
          global connections
          print "WebSocket " + str(currentid) + " closed"
          del connections[currentid]
  </code></pre>
  
  <p>I'm getting the error:</p>
  
  <pre><code>NameError: global name 'currentid' is not defined
  </code></pre>
  
  <p>on the lines of "open" and "on_close" where I print that I am opening/closing the connection. I defined it in the class, why is it not in scope. Also, I have read that using global variables is bad, but I don't see a way around this. Can someone point out what I should do? Thanks.</p>