473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Executing a list of functions


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

Mar 16 '07 #1
6 1553
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.

Mar 16 '07 #2
On Mar 16, 3:59 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
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
Mar 16 '07 #3
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
Mar 16 '07 #4
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:
pydef a():
.... print "this is a"
.... return "return value from a"
....
pydef b():
.... print "this is b"
.... return "return value from b"
....
pyfunctions = [a, b]
pyresults = [f() for f in functions]
this is a
this is b
pyprint 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:
pydef a():
.... print "this is a"
.... return "return value from a"
....
pydef b():
.... print "this is b"
.... return "return value from b"
....
pyfunctions = [a, b]
pyresults = map(apply, functions)
this is a
this is b
pyprint results
['return value from a', 'return value from b']
James
Mar 17 '07 #5
On Mar 16, 6:44 pm, James Stroud <jstr...@mbi.ucla.eduwrote:
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:

pydef a():
... print "this is a"
... return "return value from a"
...
pydef b():
... print "this is b"
... return "return value from b"
...
pyfunctions = [a, b]
pyresults = [f() for f in functions]
this is a
this is b
pyprint 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:

pydef a():
... print "this is a"
... return "return value from a"
...
pydef b():
... print "this is b"
... return "return value from b"
...
pyfunctions = [a, b]
pyresults = map(apply, functions)
this is a
this is b
pyprint results
['return value from a', 'return value from b']

James
Thanks to all for posting.

Why is apply deprecated?

jh

Mar 19 '07 #6
HMS Surprise <jo**@datavoiceint.comwrote:
...
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
Mar 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jay O'Connor | last post by:
Is there a good way to import python files without executing their content? I'm trying some relfection based stuff and I want to be able to import a module dynamically to check it's contents...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
1
by: acharyaks | last post by:
Hi Guru, the aspx page code executing behind the controls and functions executing twice. How can I avoid running the code behind the controls (for example this vanishes the control box text...
4
by: chris.dunigan | last post by:
I'm looking for an example of how to execute an existing DTS­ package from an ASP (VB)script and would appreciate any and all response. ­I don't even know if it's possible Thanks - Chuck...
2
by: Abubakar | last post by:
Hi, While debugging, can I execute functions like strlen() in immediate window? I tried strlen () on some pointer to char array and it said: CXX0017: Error: symbol "strlen" not found ...
3
by: Ara Kooser | last post by:
Hello all, I hope I am posting this correctly. I am running Python 2.4.2 on Slackware 11.0 using IDLE. I am in the process of learning python so I am writing a text adventure game using...
5
by: Gerardo Herzig | last post by:
Hi all. Im in this situation: I want to perform several kind of (validating) methods to a given value. Lets say i have a class named Number, and the following methods: is_really_a_number(),...
2
by: PatrickMinnesota | last post by:
I've been reading the docs and looking for an answer and seem stuck. I'm either not looking in the right places or not understanding what I'm reading. I have a bunch of functions. I want to put...
0
by: Orestis Markou | last post by:
Have you tried passing in empty dicts for globals and locals? I think that the defaults will be the *current* globals and locals, and then of course your namespace is broken... On Tue, Oct 7,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.