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

Discussion in 'Python' started by python@sarcastic-horse.com, Sep 16, 2003.

  1. Guest

    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?
     
    , Sep 16, 2003
    #1
    1. Advertising

  2. Re: When passing functions as args, how to pass extra args for passed function?

    On Tue, 16 Sep 2003 17:08:53 -0400 (EDT), rumours say that
    might have written:

    >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))
    --
    TZOTZIOY, I speak England very best,
    Microsoft Security Alert: the Matrix began as open source.
     
    Christos TZOTZIOY Georgiou, Sep 16, 2003
    #2
    1. Advertising

  3. Sean Ross Guest

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

    <> wrote in message
    news:...
    > 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
     
    Sean Ross, Sep 17, 2003
    #3
  4. Re: When passing functions as args, how to pass extra args for passed function?

    In article <XQM9b.1755$>,
    "Sean Ross" <> 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
     
    David Eppstein, Sep 17, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    525
    Joseph Byrns
    Apr 15, 2005
  2. Anand
    Replies:
    2
    Views:
    915
    Anand
    Sep 11, 2003
  3. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    710
    Pierre Fortin
    Sep 18, 2004
  4. mathieu
    Replies:
    3
    Views:
    608
    Bo Persson
    Sep 4, 2009
  5. Replies:
    4
    Views:
    204
    Peter J. Holzer
    Aug 16, 2013
Loading...

Share This Page