functon invoke or not
Jussi Piitulainen
jpiitula at ling.helsinki.fi
Wed Jan 9 03:55:03 EST 2013
skyworld writes:
> Hi,
>
> I see someone's code as this:
>
> class ABC: ....
> def __init__(self, env):
> .......
> self.jmpTable['batchQ']['submit_job'] = self.lsf_submit
> .......
> def lsf_submit(self, cmd,env):
> .....
>
> what confused me is why there is no parentheses for self.lsf_submit in
> "self.jmpTable['batchQ']['submit_job'] = self.lsf_submit"? what does
> this piece of code mean? thanks.
Functions are objects. The above is storing the function lsf_submit in
a dict from where it can later be taken and invoked. The invocation is
indicated by the parentheses after an expression that denotes a
function.
Consider the following, and play with examples of your own in a Python
interpreter.
>>> from math import acos
>>> def foo(x): return acos, x
...
>>> foo(-1)
(<built-in function acos>, -1)
>>> foo(-1)[0]
<built-in function acos>
>>> foo(-1)[0](foo(-1)[1])
3.141592653589793
Or simply:
>>> acos
<built-in function acos>
>>> acos(-1)
3.141592653589793
More information about the Python-list
mailing list