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

P

python

When I pass a function as an arg, like for map(...), how do I pass args to
use for that function?

If I have a function like this:

def pretty_format(f, fmt='%0.3f'):
return fmt % f

I want to use it with map() like this:

formatted = map(pretty_format, unformatted_list)
#exept I want fmt='%4.5f' !!!

I need to figure out how to pass a non-default value for fmt. How do I do
that?
 
C

Christos TZOTZIOY Georgiou

When I pass a function as an arg, like for map(...), how do I pass args to
use for that function?

If I have a function like this:

def pretty_format(f, fmt='%0.3f'):
return fmt % f

I want to use it with map() like this:

formatted = map(pretty_format, unformatted_list)
#exept I want fmt='%4.5f' !!!

I need to figure out how to pass a non-default value for fmt. How do I do
that?

I believe this is what you want:

formatted = map(pretty_format, unformatted_list,
['%4.5f'] * len(unformatted_list))
 
S

Sean Ross

When I pass a function as an arg, like for map(...), how do I pass args to
use for that function?

If I have a function like this:

def pretty_format(f, fmt='%0.3f'):
return fmt % f

I want to use it with map() like this:

formatted = map(pretty_format, unformatted_list)
#exept I want fmt='%4.5f' !!!

I need to figure out how to pass a non-default value for fmt. How do I do
that?


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

HTH
Sean
 
D

David Eppstein

"Sean Ross said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top