title: Dictionary of functions for all function in class
body: <p>I'm relatively new to python and I have a class that has a bunch of different function. I read in the user input and depending on the user input I call a different function. Instead of having a bunch of if else statements I thought it would be better to have a dictionary of functions so currently my class looks like this:</p>
  
  <pre><code>class Foo:
  
    def func1(self):
      #do something
  
    def func2(self, arg1):
      #do something else
  
    def func3(self, arg1, arg2):
     #do something
  
    def func4(self, arg1):
      #do something
  
    def __init__(self):
      self.functions = {"FUNC2": func2, "FUNC4":func4}
  
    def run_loop(self):
      while 1:
        user_input = raw_input()
  
        cmd = user_input.split(' ')[0]
  
        if cmd in self.functions:
          self.functions[cmd].__get__(self, type(self))()
        else:
          #call other functions
  </code></pre>
  
  <p>I'm calling this in a <code>main.py</code> like so:</p>
  
  <pre><code>c = Class()
  
  c.run_loop()
  </code></pre>
  
  <p>My issue is that I'm getting the following error <code>NameError</code>: global name 'func2' is not defined`. I'm not really sure why this is happening. I get the error in the constructor. Any ideas?</p>