When passing functions as args,
Mel Wilson
mwilson at the-wire.com
Wed Sep 17 08:14:11 EDT 2003
In article <mailman.1063746594.3920.python-list at python.org>,
python at sarcastic-horse.com wrote:
>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?
map (lambda x: pretty_format (x, '%4.5f'), unformatted_list)
or
[pretty_format (x, '%4.5f') for f in unformatted_list]
should do the trick. Actually filling in the default
argument is not simple, and probably isn't radically better
than the above.
Unless you're going to put more functionality into
pretty_format, or you have some other reason to centralize
your formatting,
['%4.5f' % x for x in unformatted_list]
might be best of all.
Regards. Mel.
More information about the Python-list
mailing list