functon invoke or not

Peter Otten __peter__ at web.de
Wed Jan 9 09:43:15 CET 2013


skyworld wrote:

> Hi,
> 
> I see someone's code as this:
> 
> class ABC: ....
>     def __init__(self, env):
>          .......
>          self.jmpTable['batchQ']['submit_job']  = self.lsf_submit

The bound method self.lsf_submit is not invoked in this line, it is stored 
for later use.

>          .......
>     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.

Somewhere else in the code the is probably code similar to

var1 = ...
var2 = ...
self.jmpTable[var1][var2](some_command, some_env)

When var1 is "batchQ" and var2 is "submit_job" this will in effect call

self.lsf_submit(some_command, some_env)

A slightly simplified example with just a dict and two functions:

>>> def german(name):
...     print "Guten Tag, Herr", name
... 
>>> def french(name):
...     print "Bonjour, M.", name
... 
>>> lookup = {"fr": french, "de": german}
>>> lookup["fr"]("Hulot")
Bonjour, M. Hulot





More information about the Python-list mailing list