473,587 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with map function

In the book 'Text Processing Python,' they present a function that I'm
having a real hard time understanding.

apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))
I understand that the lamda statement is passing a function that
accepts a function and an iterable variable as arguments.

The function passed by lamda is a map statement that maps the function
in the arguement list against each item in the iterable variable.

However, I don't understand what is happening in the 3rd arguement of
map though. What does [args]*len(fns) do?
Jul 18 '05 #1
4 1603
Michael wrote:
In the book 'Text Processing Python,' they present a function that I'm
having a real hard time understanding.

apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))
A lambda creates an anonymous function. Assigning a lambda directly to a
variable is simply an exercise in obfuscation. The same code can be more
clearly expressed:

def apply_each(fns, args=[]):
return map(apply, fns, [args]*len(fns))
These days you can express the same thing more clearly with a list
comprehension:

def apply_each(fns, args=[]):
return [ fn(*args) for fn in fns ]

In general it would probably be better to define it as:

def apply_each(fns, *args, **kw):
return [ fn(*args, **kw) for fn in fns ]

although this changes slightly how you call it.

I understand that the lamda statement is passing a function that
accepts a function and an iterable variable as arguments.

The function passed by lamda is a map statement that maps the function
in the arguement list against each item in the iterable variable.

However, I don't understand what is happening in the 3rd arguement of
map though. What does [args]*len(fns) do?

[args]*len(fns) is equivalent to [args, args, args, ...] where args is
repeated as often as there are elements in the list. It means that every
time apply is called it gets one of the functions and the args value as its
parameters.







Jul 18 '05 #2
Duncan Booth wrote:
A lambda creates an anonymous function. Assigning a lambda directly to a variable is simply an exercise in obfuscation. The same code can be more clearly expressed:

def apply_each(fns, args=[]):
return map(apply, fns, [args]*len(fns))
I agree. I was so busy trying to wrap my head around this statement,
that I realize it was more complex than it had to be.
These days you can express the same thing more clearly with a list
comprehension:

def apply_each(fns, args=[]):
return [ fn(*args) for fn in fns ]

In general it would probably be better to define it as:

def apply_each(fns, *args, **kw):
return [ fn(*args, **kw) for fn in fns ]

although this changes slightly how you call it.

I don't have any experience working with *args, **kw arguments. From
what I've read, *args allows for an unknown number of arguments to
passed into a function and **kw allows for an unknown number of
key-value pairs to passed into a function. But from what I understand,
both are optional. This shouldn't change the how you call apply_each.
[args]*len(fns) is equivalent to [args, args, args, ...] where args is repeated as often as there are elements in the list. It means that every time apply is called it gets one of the functions and the args value as its parameters.


I understand now. [args] can be a list of variables, including a list
of lists. Each variable within [args] is passed to the function called
within apply_each().

Jul 18 '05 #3
> I don't have any experience working with *args, **kw arguments.
From what I've read, *args allows for an unknown number of
arguments to passed into a function and **kw allows for an
unknown number of key-value pairs to passed into a function.
But from what I understand, both are optional. This shouldn't
change the how you call apply_each.


I spoke too soon. If the function being called doesn't provide for
*args, or **kw then a TypeError exception will be thrown.

Jul 18 '05 #4
wrote:
I don't have any experience working with *args, **kw arguments.
From what I've read, *args allows for an unknown number of
arguments to passed into a function and **kw allows for an
unknown number of key-value pairs to passed into a function.
But from what I understand, both are optional. This shouldn't
change the how you call apply_each.


I spoke too soon. If the function being called doesn't provide for
*args, or **kw then a TypeError exception will be thrown.


The original apply_each expected you to use it something like this:

def fn1(a,b,c): ... whatever ...
def fn2(a,b,c): ... whatever ...

result = apply_each([fn1, fn2], [1, 2, 3])

A revised version:

def apply_each(fns, *args, **kw):
return [ fn(*args, **kw) for fn in fns ]

could be called in a variety of ways:

result = apply_each([fn1, fn2], 1, 2, 3)
result = apply_each([fn1, fn2], *[1, 2, 3])
result = apply_each([fn1, fn2], a=1, c=3, b=2)
result = apply_each([fn1, fn2], **{'a': 1, 'c': 3, 'b': 2})

The functions actually being called don't need to know anything about how
they are invoked, they just need to get a suitable combination of
positional and/or keyword arguments.
Jul 18 '05 #5

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

Similar topics

7
2378
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the...
2
3872
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at http://home.pacific.net.sg/~jacksony ? (the drop down bar could not work at www.apchosting.net but can drop at home.pacific.net.sg. I suspect it is a server problem but was told it...
5
2981
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
6
4974
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a...
1
1959
by: Michael D. Reed | last post by:
I am using the help class to display a simple help file. I generated the help file using Word and saving it as a single page Web page (.mht extension). I show the help file with the following statement. Help.ShowHelp(Parent:=Me, url:=Me.HELP_URL_PRE & Me.myWorker.HelpFile) How do I get it to go away when the program exits? Now when I quit...
1
3700
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am...
0
5546
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
4
1502
by: Stef Mientki | last post by:
I'm making special versions of existing functions, and now I want the help-text of the newly created function to exists of 1. an extra line from my new function 2. all the help text from the old function # the old function def triang(M,sym=1): """The M-point triangular window. <== old help text """ ....
22
3245
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
1
4938
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: http://munich.schwarz-interactive.de/autocomplete.aspx
0
7923
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...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
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. ...
0
8349
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...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
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...
0
3845
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...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.