title: The self parameter with a dictionary of functions within a class
body: <p>I'm attempting to create a dictionary of executable functions within a class. But having trouble getting the self parameter to work correctly.</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>class myclass(object):
  
    def x(self):
     return 'x'
  
    def y(self):
     return 'y'
  
    EF= {
     'a':x,
     'b':y,
    }
  
    def test(self):
      print self.EF['a']()
  </code></pre>
  
  <p>When I attempt to execute the 'test' function of the class, I get an error around the number of parameters as it evaluates and executes one of the functions in the dictionary.</p>
  
  <pre><code>&gt;&gt;&gt; class myclass(object):
  ...   def x(self):
  ...    return 'x'
  ...   def y(self):
  ...    return 'y'
  ...   EF= {
  ...    'a':x,
  ...    'b':y,
  ...   }
  ...   def test(self):
  ...     print self.EF['a']()
  ...
  &gt;&gt;&gt;
  &gt;&gt;&gt;
  &gt;&gt;&gt; m=myclass()
  &gt;&gt;&gt; m.test()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 11, in test
  TypeError: x() takes exactly 1 argument (0 given)
  </code></pre>
  
  <p>I've tried a few variations, including this which doesn't work.</p>
  
  <pre><code>  EF= {
     'a':self.x,
     'b':self.y,
    }
  </code></pre>
  
  <p>The only thing that did work was when I explicitly passed self as a parameter, like this.</p>
  
  <pre><code>...   def test(self):
  ...     print self.EF['a'](self)
  ...
  &gt;&gt;&gt; m=myclass()
  &gt;&gt;&gt; m.test()
  x
  </code></pre>
  
  <p>I've seen other questions about using a dictionary to index functions, but none from within a class.</p>
  
  <p>Here are my questions:</p>
  
  <ul>
  <li>What is the proper way to do handle the self parameter? </li>
  <li>I'd prefer to move my dictionary constant outside of the class into my constants section. Can I do that, and if so how?  Should I do that?  </li>
  <li>If I should/have to have my dictionary within my class, why can't I move it to the top of the class?</li>
  </ul>
  
  <p>That's all I got. Thanks for the help.</p>