473,715 Members | 6,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_form at, unformatted_lis t)
#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 2377
On Tue, 16 Sep 2003 17:08:53 -0400 (EDT), rumours say that
py****@sarcasti c-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_form at, unformatted_lis t)
#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_form at, unformatted_lis t,
['%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****@sarcast ic-horse.com> wrote in message
news:ma******** *************** **********@pyth on.org...
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_form at, unformatted_lis t)
#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_lis t)
And, here's another (requires itertools, available in Python 2.3[*]):

from itertools import repeat
formatted = map(pretty_form at, 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***@connectm ail.carleton.ca > wrote:
Here's one way:

formatted = map(lambda f: pretty_format(f , fmt='%4.5f'), unformatted_lis t)
And, here's another (requires itertools, available in Python 2.3[*]):

from itertools import repeat
formatted = map(pretty_form at, 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,**kwar gs)

map(curry(prett y_format, fmt='%4.5f'), unformatted_lis t)

--
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
1615
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 of the Car object and the name of one of its methods. I found one way to get this to work but can someone show me a more orthodox way? Is there a way using references to the object and somehow the method? Python 2.2.2 (#37, Oct 14 2002, 17:02:34)...
58
10157
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 code... TCHAR myArray; DoStuff(myArray);
7
9566
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 subroutine Let's say theres a sub that creates buttons and I want it to receive as a parameter the address of the sub that handles the OnClick event for the button being created How do I pass such a parameter Thanks in advance Richar
7
1971
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 passed, 0, 1, or n arguments... ... //call another function... var someCalculatedValue = 'someValue';
4
1509
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 tokens you're looking for). In another case I have a function that handles a different set of tokens and so it can't use the same arguments as the first one, and in fact takes no arguments. However, these functions are semantically similar and are...
18
4352
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
3
1349
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, "(ii)(ii)", &faceId1, &vertId1, &faceId2, &vertId2) I want the to change the faceId's in my function. From what I've seen you can't do this safely with PyArg_ParseTuple.
2
1090
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 them in a list. Then I want to pass that list into another function which does some setup and then loops through the list of passed in functions and executes them. Some of them need arguments passed in too. Can someone point me to where to...
9
3989
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 (most recent call last):
0
8823
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9343
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9198
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.