What function is func here? Where did it come from?
<p>This is my code:</p>

<pre><code>def testit(func, *nkwargs, **kwargs):
     try:
        retval = func(*nkwargs, **kwargs)
        result = (True, retval)
     except Exception, diag:
        result = (False, str(diag))
     return result

def test():
     funcs = (int, long, float)
     vals = (1234, 12.34, '1234', '12.34')

     for eachFunc in funcs:
        print '-' * 20
        for eachVal in vals:
            retval = testit(eachFunc, eachVal)
            if retval[0]:
               print '%s(%s) =' % \
                     (eachFunc.__name__, `eachVal`), retval[1]
            else:
               print '%s(%s) = FAILED:' % \
                     (eachFunc.__name__, `eachVal`), retval[1]

if __name__ == '__main__':
    test()
</code></pre>

<p>What is the function of the <code>func</code> in the third line. I think it is a variable. How did it become a function name?</p>

<p>Python functions are first-class objects. This means you can assign such an object to a variable and pass it to another function. The very act of executing a <code>def functionname(): ...</code> statement assigns such an object to a name (<code>functionname</code> here):</p>

<pre><code>&gt;&gt;&gt; def foo(): return 42
...
&gt;&gt;&gt; foo
&lt;function foo at 0x107e9f0d0&gt;
&gt;&gt;&gt; bar = foo
&gt;&gt;&gt; bar
&lt;function foo at 0x107e9f0d0&gt;
&gt;&gt;&gt; bar()
42
</code></pre>

<p>So the expression</p>

<pre><code>funcs = (int, long, float)
</code></pre>

<p>takes 3 built-in functions and puts those into a tuple. <code>funcs[0]('10')</code> would return the integer object <code>10</code>, because <code>funcs[0]</code> is another reference to the <code>int()</code> function, so <code>funcs[0]('10')</code> would give you the exact same outcome as <code>int('10')</code>.</p>

<p>Those function objects are passed to the <code>testit()</code> function in a loop with:</p>

<pre><code> for eachFunc in funcs:
    print '-' * 20
    for eachVal in vals:
        retval = testit(eachFunc, eachVal)
</code></pre>

<p>So <code>eachFunc</code> is bound to the function objects that the <code>funcs</code> tuple references, one by one.</p>

<p><code>testit()</code> takes that function object as the <code>func</code> parameter, then calls it:</p>

<pre><code>def testit(func, *nkwargs, **kwargs):
     try:
        retval = func(*nkwargs, **kwargs)
</code></pre>

<p>so this calls <code>int()</code>, <code>long()</code> and <code>float()</code> on various test values, from the <code>values</code> tuple.</p>

python function