473,699 Members | 2,146 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 1085
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
10141
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
2839
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 pieces of information in the HTTP Module and make it available to the executing instance of the page in...
7
9563
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 for the button being created How do I pass such a parameter Thanks in advance Richar
12
5330
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 the windows application. I face the following problem when I send this class to the webservice.
23
4005
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 obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
15
22882
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 table. I am a little new to SQL Server, but I have some experience with other RDBMS. I get an...
11
4128
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
1968
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
1590
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 events once and only if there’s at least one client subscriber. Once the last client subscriber...
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9048
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8892
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7767
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6539
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4386
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4635
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.