Access Functions via Dictionary
<p>I have a function like this:</p>

<pre><code>def abc(a,b):
    return a+b
</code></pre>

<p>And I want to assign it to a dictionary like this:</p>

<pre><code>functions = {'abc': abc(a,b)}
</code></pre>

<p>The trouble is, when assigning it to the dictionary, since the arguments are not yet defined, I get the error: </p>

<pre><code>NameError: name 'a' is not defined
</code></pre>

<p>I would do the obvious thing and define the arguments ahead of time but I need to define them in a loop (and then call the function based on locating it in a list) like this:</p>

<pre><code>functions_to_call = ['abc']
for f in functions_to_call:
    a=3
    b=4
#This is supposed to locate and run the function from the dictionary if it is in the list of functions.
    if f in functions:
       functions[f] 
</code></pre>

<blockquote>
  <p>I need to define them in a loop (and then call the function based on locating it in a list)</p>
</blockquote>

<p>Then what's the issue with simply saving the function object in the dictionary:</p>

<pre><code>functions = {'abc':abc}
</code></pre>

<p>and <em>then</em> applying <code>a</code> and <code>b</code> to the function while looping:</p>

<pre><code>functions_to_call = ['abc']
for f in functions_to_call:
    a, b = 3, 4
    if f in functions:
       functions[f](a, b)
</code></pre>

python list function python-3.x dictionary