Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   functon invoke or not (http://www.velocityreviews.com/forums/t956334-functon-invoke-or-not.html)

skyworld 01-09-2013 08:23 AM

functon invoke or not
 
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.

Peter Otten 01-09-2013 08:43 AM

Re: functon invoke or not
 
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



Mitya Sirenef 01-09-2013 08:46 AM

Re: functon invoke or not
 
On Wed 09 Jan 2013 03:23:56 AM EST, skyworld wrote:
> 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


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

skyworld 01-09-2013 08:50 AM

Re: functon invoke or not
 
On 1月9日, 下午4时46分, Mitya Sirenef <msire...@lightbird.net> wrote:
> On Wed 09 Jan 2013 03:23:56 AM EST, skyworld wrote:
>
> > 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"? 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
>
> --
> Lark's Tongue Guide to Python:http://lightbird.net/larks/


Thanks for both of your replies. I got it.

Jussi Piitulainen 01-09-2013 08:55 AM

Re: functon invoke or not
 
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


All times are GMT. The time now is 06:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.