473,563 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing functions around and executing

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 them in a list. Then I
want to pass that list into another function which does some setup and
then loops through the list of passed in functions and executes them.
Some of them need arguments passed in too.

Can someone point me to where to read about this? I know it's do-able
since it's basically doing something like a callback would do.

Thanks for any pointers.
Jun 27 '08 #1
2 1081
On May 15, 10:53 am, PatrickMinnesot a <PatrickMinnes. ..@gmail.com>
wrote:
I have a bunch of functions. I want to put them in a list. Then I
want to pass that list into another function which does some setup and
then loops through the list of passed in functions and executes them.
Some of them need arguments passed in too.
Hey Patrick,

Is something like the following helpful?
>>def fn1(): print 'fn1'
def fn2(): print 'fn2'
fn_list = [fn1, fn2]
def process(fn_seq) :
.... # do set up here
.... for fn in fn_list:
.... fn()
>>process(fn_li st)
fn1
fn2

The easiest way to extend this for optional argument passing would be
to have each function accept keyword arguments, and then to pass a
dictionary of arguments in to each:
>>def fn1(**kwargs): print 'fn1'
def fn2(**kwargs): print 'fn2: x=%(x)s' % kwargs
fn_list = [fn1, fn2]
def process(fn_seq) :
.... x = 'hello'
.... for fn in fn_list:
.... fn(**locals())
>>process(fn_li st)
fn1
fn2: x=hello

You could replace 'process' with a list comprehension:
>>args = dict(x='hello again')
results = [f(**args) for f in fn_list]
fn1
fn2: x=hello again

Or use 'map':
>>process = lambda f: f(**args)
results = map(process, fn_list)
fn1
fn2: x=hello again

Sorry, I'm a little bored.

- alex23
Jun 27 '08 #2
alex23 <wu*****@gmail. comwrites:
On May 15, 10:53 am, PatrickMinnesot a <PatrickMinnes. ..@gmail.com>
wrote:
>I have a bunch of functions. I want to put them in a list. Then I
want to pass that list into another function which does some setup and
then loops through the list of passed in functions and executes them.
Some of them need arguments passed in too.

Hey Patrick,

Is something like the following helpful?
>>>def fn1(): print 'fn1'
def fn2(): print 'fn2'
fn_list = [fn1, fn2]
def process(fn_seq) :
... # do set up here
... for fn in fn_list:
... fn()
>>>process(fn_l ist)
fn1
fn2

The easiest way to extend this for optional argument passing would be
to have each function accept keyword arguments, and then to pass a
dictionary of arguments in to each:
Or you could wrap your functions in functools.parti al:

def foo(n):
return 'x'*n
>>from functools import partial
foo10 = partial(foo, 10)
print foo10()
xxxxxxxxxx

HTH

--
Arnaud
Jun 27 '08 #3

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

Similar topics

58
10077
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
1
2836
by: Michael S. Jones | last post by:
I am thinking through a framework for logging, authentication, etc. that requires the system to look up quite a bit of information from a database. Is there an easy way to pass information that is retrieved in an HTTP Module to the executing page instance in ASP.NET? For example, I would like to create an object that would store various...
7
9545
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a subroutine Let's say theres a sub that creates buttons and I want it to receive as a parameter the address of the sub that handles the OnClick event...
12
5307
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and...
23
3978
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some...
15
22874
by: Steve | last post by:
I am trying to develop a stored procedure for an existing application that has data stored in numerous tables, each with the same set of columns. The main columns are Time and Value. There are literally hundreds of these tables that are storing values at one minute intervals. I need to calculate the value at the end of the current hour for any...
11
4116
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing the actual structure itself. Is this true of C# also?
1
1960
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. function foo2(){} function foo(func){}
2
1581
by: Israel | last post by:
It seems like there should be a way to pass events around like delegates but on the other hand they’re sort of special delegates because the only one allowed to invoke them is the owner of the event. What I want to do is create a generic event forwarder that forwards events from a server to a client. It will only subscribe to the server...
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.