When passing functions as args, how to pass extra args for passed function?

David Eppstein eppstein at ics.uci.edu
Tue Sep 16 20:25:20 EDT 2003


In article <XQM9b.1755$Ie5.334063 at news20.bellglobal.com>,
 "Sean Ross" <sross at connectmail.carleton.ca> wrote:

> Here's one way:
> 
> formatted = map(lambda f: pretty_format(f, fmt='%4.5f'), unformatted_list)
> 
> 
> And, here's another (requires itertools, available in Python 2.3 [*]):
> 
> from itertools import repeat
> formatted = map(pretty_format, unformated_list, repeat('%4.5f'))
> 
> 
> [*]
> # or you can roll your own,
> # credit: http://www.python.org/dev/doc/devel/lib/itertools-functions.html
> def repeat(object, times=None):
>          if times is None:
>              while True:
>                  yield object
>          else:
>              for i in xrange(times):
>                  yield object
> 
> # may need 'from __future__ import generators' at top of file

def curry(f, **kwargs):
    return lambda *largs: f(*largs,**kwargs)

map(curry(pretty_format, fmt='%4.5f'), unformatted_list)

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list