functon invoke or not

Discussion in 'Python' started by skyworld, Jan 9, 2013.

  1. skyworld

    skyworld Guest

    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.
    skyworld, Jan 9, 2013
    #1
    1. Advertising

  2. skyworld

    Peter Otten Guest

    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
    Peter Otten, Jan 9, 2013
    #2
    1. Advertising

  3. 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/
    Mitya Sirenef, Jan 9, 2013
    #3
  4. skyworld

    skyworld Guest

    On 1月9æ—¥, 下åˆ4æ—¶46分, Mitya Sirenef <> 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.
    skyworld, Jan 9, 2013
    #4
  5. 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
    Jussi Piitulainen, Jan 9, 2013
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jerry051
    Replies:
    2
    Views:
    738
    Oliver Sturm
    Aug 2, 2005
  2. Neo
    Replies:
    2
    Views:
    3,197
    bruce barker
    May 11, 2004
  3. Malik Asif Joyia
    Replies:
    2
    Views:
    1,081
    =?Utf-8?B?ZGFuaWVs?=
    Aug 12, 2005
  4. =?Utf-8?B?QWxleA==?=
    Replies:
    2
    Views:
    694
    =?Utf-8?B?QWxleA==?=
    Dec 24, 2005
  5. if you inline a functon

    , Nov 21, 2007, in forum: C Programming
    Replies:
    4
    Views:
    273
    Martin Ambuhl
    Nov 21, 2007
Loading...

Share This Page