Executing a list of functions

H

HMS Surprise

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
 
7

7stud

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

7stud

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
 
M

Michel Claveau

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]
 
J

James Stroud

HMS said:
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
 
H

HMS Surprise

HMS said:
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?



def a():
print "this is a"
def b():
print "this is b"
lst = [a(), b()]

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
 
A

Alex Martelli

HMS Surprise said:
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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top