functon invoke or not

S

skyworld

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

Peter Otten

skyworld said:
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:
.... print "Guten Tag, Herr", name
........ print "Bonjour, M.", name
....
lookup = {"fr": french, "de": german}
lookup["fr"]("Hulot")
Bonjour, M. Hulot
 
M

Mitya Sirenef

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.


Presumably it will be called at a later point:

def f(): print 'foo'

lst = [f]
# la la
lst[0]()


HTH, -m
 
S

skyworld

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"? whatdoes
this piece of code mean? thanks.

Presumably it will be called at a later point:

def f(): print 'foo'

lst = [f]
# la la
lst[0]()

HTH,  -m

Thanks for both of your replies. I got it.
 
J

Jussi Piitulainen

skyworld said:
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)
foo(-1)[0]
foo(-1)[0](foo(-1)[1])
3.141592653589793

Or simply:
3.141592653589793
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top