Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing functions around and executing

Reply
Thread Tools

Passing functions around and executing

 
 
PatrickMinnesota
Guest
Posts: n/a
 
      05-15-2008
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.
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      05-15-2008
On May 15, 10:53 am, PatrickMinnesota <(E-Mail Removed)>
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_list)

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_list)

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
 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      05-15-2008
alex23 <(E-Mail Removed)> writes:

> On May 15, 10:53 am, PatrickMinnesota <(E-Mail Removed)>
> 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_list)

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

def foo(n):
return 'x'*n

>>> from functools import partial
>>> foo10 = partial(foo, 10)
>>> print foo10()

xxxxxxxxxx

HTH

--
Arnaud
 
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
WTB: I BUY SOFTWARE - CHECK AROUND - YOU PROBABLY HAVE SOME OF THEBELOW LAYING AROUND. Network/Software Buyer C++ 0 05-23-2010 07:14 PM
Microsoft & patents: what goes around comes around... Lawrence D'Oliveiro NZ Computing 104 12-16-2006 07:11 AM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Make wxListCtrl fit around contents and parent frame fit around listctrl Piet Python 0 07-18-2004 08:27 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments