Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Executing a list of functions

Reply
Thread Tools

Executing a list of functions

 
 
HMS Surprise
Guest
Posts: n/a
 
      03-16-2007

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

 
Reply With Quote
 
 
 
 
7stud
Guest
Posts: n/a
 
      03-16-2007
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.

 
Reply With Quote
 
 
 
 
7stud
Guest
Posts: n/a
 
      03-16-2007
On Mar 16, 3:59 pm, "7stud" <bbxx789_0...@yahoo.com> 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


 
Reply With Quote
 
Michel Claveau
Guest
Posts: n/a
 
      03-16-2007
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


 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      03-17-2007
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
 
Reply With Quote
 
HMS Surprise
Guest
Posts: n/a
 
      03-19-2007
On Mar 16, 6:44 pm, James Stroud <jstr...@mbi.ucla.edu> 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

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      03-19-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
executing bluez functions in java Mithil Java 3 04-12-2007 03:05 PM
Functions executing on Page Load? cider123@hotmail.com ASP .Net 2 05-15-2006 09:10 PM
ASP.NET not executing interop functions =?Utf-8?B?SmFzIE1hbmdoZXJh?= ASP .Net 2 04-06-2005 05:05 PM
Variadic functions calling variadic functions with the argument list, HLL bit shifts on LE processors Ross A. Finlayson C Programming 19 03-10-2005 03:57 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57