Executing a list of functions

Discussion in 'Python' started by HMS Surprise, Mar 16, 2007.

  1. HMS Surprise

    HMS Surprise Guest

    Seems to me that one should be able to put the names of several
    functions in a list and then have the list executed. But it seems the
    output of the functions is hidden, only their return value is visible.
    Is this because the list execution is another scope?

    Thanx,

    jh

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def a():
    print "this is a"

    def b():
    print "this is b"

    lst = [a(), b()]

    lst
    HMS Surprise, Mar 16, 2007
    #1
    1. Advertising

  2. HMS Surprise

    7stud Guest

    lst = [a, b]

    The () symbol causes the named function to execute, and a function
    call in the code is always replaced by the function's return value.
    7stud, Mar 16, 2007
    #2
    1. Advertising

  3. HMS Surprise

    7stud Guest

    On Mar 16, 3:59 pm, "7stud" <> wrote:
    > lst = [a, b]
    >
    > The () symbol causes the named function to execute, and a function
    > call in the code is always replaced by the function's return value.


    Try this:

    ------
    def a():
    print "this is a"

    def b():
    print "this is b"

    lst = [a(), b()]
    ------

    To create the list, the terms inside the list have to be evaluated.
    That causes a and b to execute, and the function calls are replaced by
    the each function's return value. Since your functions don't have a
    return statement, the value None is returned. To see that, add this
    line:

    print lst
    7stud, Mar 16, 2007
    #3
  4. Hi!

    Your code run OK for me.

    But, if you want "time-lag" (sorry for my english) execution, you can
    try this:


    def a():
    print "this is a"

    def b():
    print "this is b"

    lst = [a, b]
    [f() for f in lst]







    --
    @-salutations

    Michel Claveau
    Michel Claveau, Mar 16, 2007
    #4
  5. HMS Surprise

    James Stroud Guest

    HMS Surprise wrote:
    > Seems to me that one should be able to put the names of several
    > functions in a list and then have the list executed. But it seems the
    > output of the functions is hidden, only their return value is visible.
    > Is this because the list execution is another scope?
    >
    > Thanx,
    >
    > jh
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > def a():
    > print "this is a"
    >
    > def b():
    > print "this is b"
    >
    > lst = [a(), b()]
    >
    > lst
    >


    The "print" statement does nothing to return a value from the function,
    so the strings "this is *" will not be stored in your list. They will,
    however, be printed if you are some how observing your program output
    (e.g. running it in IDLE, or a command shell).

    To save the "results" of the functions, you need to produce results,
    which means actually using "return" to return some value. Here is an
    example:


    def a():
    print "this is a"
    return "return value from a"

    def b():
    print "this is b"
    return "return value from b"

    functions = [a, b]
    results = [f() for f in functions]
    print results


    Here is the result of this example:


    py> def a():
    .... print "this is a"
    .... return "return value from a"
    ....
    py> def b():
    .... print "this is b"
    .... return "return value from b"
    ....
    py> functions = [a, b]
    py> results = [f() for f in functions]
    this is a
    this is b
    py> print results
    ['return value from a', 'return value from b']


    A fun, but unfortunately deprecated, way to do this is with the "apply"
    function in conjunction with the "map" function:


    def a():
    print "this is a"
    return "return value from a"

    def b():
    print "this is b"
    return "return value from b"

    functions = [a, b]
    results = map(apply, functions)
    print results


    Here is this example at work:


    py> def a():
    .... print "this is a"
    .... return "return value from a"
    ....
    py> def b():
    .... print "this is b"
    .... return "return value from b"
    ....
    py> functions = [a, b]
    py> results = map(apply, functions)
    this is a
    this is b
    py> print results
    ['return value from a', 'return value from b']


    James
    James Stroud, Mar 17, 2007
    #5
  6. HMS Surprise

    HMS Surprise Guest

    On Mar 16, 6:44 pm, James Stroud <> wrote:
    > HMS Surprise wrote:
    > > Seems to me that one should be able to put the names of several
    > > functions in a list and then have the list executed. But it seems the
    > > output of the functions is hidden, only their return value is visible.
    > > Is this because the list execution is another scope?

    >
    > > Thanx,

    >
    > > jh

    >
    > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    >
    > > def a():
    > > print "this is a"

    >
    > > def b():
    > > print "this is b"

    >
    > > lst = [a(), b()]

    >
    > > lst

    >
    > The "print" statement does nothing to return a value from the function,
    > so the strings "this is *" will not be stored in your list. They will,
    > however, be printed if you are some how observing your program output
    > (e.g. running it in IDLE, or a command shell).
    >
    > To save the "results" of the functions, you need to produce results,
    > which means actually using "return" to return some value. Here is an
    > example:
    >
    > def a():
    > print "this is a"
    > return "return value from a"
    >
    > def b():
    > print "this is b"
    > return "return value from b"
    >
    > functions = [a, b]
    > results = [f() for f in functions]
    > print results
    >
    > Here is the result of this example:
    >
    > py> def a():
    > ... print "this is a"
    > ... return "return value from a"
    > ...
    > py> def b():
    > ... print "this is b"
    > ... return "return value from b"
    > ...
    > py> functions = [a, b]
    > py> results = [f() for f in functions]
    > this is a
    > this is b
    > py> print results
    > ['return value from a', 'return value from b']
    >
    > A fun, but unfortunately deprecated, way to do this is with the "apply"
    > function in conjunction with the "map" function:
    >
    > def a():
    > print "this is a"
    > return "return value from a"
    >
    > def b():
    > print "this is b"
    > return "return value from b"
    >
    > functions = [a, b]
    > results = map(apply, functions)
    > print results
    >
    > Here is this example at work:
    >
    > py> def a():
    > ... print "this is a"
    > ... return "return value from a"
    > ...
    > py> def b():
    > ... print "this is b"
    > ... return "return value from b"
    > ...
    > py> functions = [a, b]
    > py> results = map(apply, functions)
    > this is a
    > this is b
    > py> print results
    > ['return value from a', 'return value from b']
    >
    > James


    Thanks to all for posting.

    Why is apply deprecated?

    jh
    HMS Surprise, Mar 19, 2007
    #6
  7. HMS Surprise <> wrote:
    ...
    > Why is apply deprecated?


    Because it does exacly the same job as just calling the function with
    *a/**k, and there should preferably be only one obvious way to perform a
    given task (this guiding principle leads to simplicity in the language,
    and is common to Python and to the "Spirit of C" as explained in the
    preface of the ISO C Standard -- they phrase it as "offer only one way
    to perform an operation", I believe).


    Alex
    Alex Martelli, Mar 19, 2007
    #7
    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. =?Utf-8?B?SmFzIE1hbmdoZXJh?=

    ASP.NET not executing interop functions

    =?Utf-8?B?SmFzIE1hbmdoZXJh?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    442
    =?Utf-8?B?SmFzIE1hbmdoZXJh?=
    Apr 6, 2005
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,570
    Victor Bazarov
    Jun 21, 2004
  3. Replies:
    2
    Views:
    567
  4. Ross A. Finlayson
    Replies:
    19
    Views:
    568
    Keith Thompson
    Mar 10, 2005
  5. Mithil
    Replies:
    3
    Views:
    2,322
    Mithil
    Apr 12, 2007
Loading...

Share This Page