473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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?

Jul 18 '05 #1
3 2355
On Tue, 16 Sep 2003 17:08:53 -0400 (EDT), rumours say that
py****@sarcastic-horse.com 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.
Jul 18 '05 #2
<py****@sarcastic-horse.com> wrote in message
news:ma*********************************@python.or g...
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/...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
Jul 18 '05 #3
In article <XQ*******************@news20.bellglobal.com>,
"Sean Ross" <sr***@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/...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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Midas | last post by:
Greetings everyone, I'm including code, for cut/paste, to better explain my question. I create a Car object then I call some of its methods. No problem. Then I try to pass, to a function, the name...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
7
by: Bonzo | last post by:
>From within a function, I want to pass a/some parameters to another function, AND all arguments, passed into this function. e.g. function firstFunction(){ //this function may have been...
4
by: 63q2o4i02 | last post by:
Hi, I'm writing a hand-written recursive decent parser for SPICE syntax parsing. In one case I have one function that handles a bunch of similar cases (you pass the name and the number of...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
3
by: stuart.tett | last post by:
I'm writing my own python extension module with the C API. In python all functions pass arguments by reference, but how can I make use of this in C? Right now, I am using: PyArg_ParseTuple(args,...
2
by: PatrickMinnesota | last post by:
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...
9
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.