473,387 Members | 1,481 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,387 software developers and data experts.

Can someone give me a short explanation?

I found this code in the book text processing with Python. But having little
difficulty understanding how exactly it works and what should be passed as
argument? Can someone decode this for me please?

apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))

Senthoor

__________________________________________________ _______________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail
Jul 18 '05 #1
4 1442
Senthoorkumaran Punniamoorthy wrote:
I found this code in the book text processing with Python. But having
little difficulty understanding how exactly it works and what should be
passed as argument? Can someone decode this for me please?

apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))


This can be rewritten to (untested):

def apply_each(fns, args=[]):
return map(apply, fns, [args]*len(fns))

Now we see that the intermediate list containing args len(fns) times, i. e.
once for every function passed in fns is superfluous:

def apply_each(fns, args=[])
return map(lambda f: apply(f, args), fns)

or written in modern style:

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

It should be clear by now what this does: Call each function in the fns
sequence with the items in args as arguments, the default being no
arguments. Note that I find the list comprehension so readable that I'd be
tempted to put it literally into my code; so in a way we are back at the
beginning - only much better :-)

Peter
Jul 18 '05 #2
"Senthoorkumaran Punniamoorthy" <so***********@hotmail.com> writes:
I found this code in the book text processing with Python. But having
little difficulty understanding how exactly it works and what should
be passed as argument? Can someone decode this for me please?
apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))

"map" is a builtin which applies its first argument (which should be a
function[*]) to the elements coming out of the sequences which are passed
to it as arguments 2,3, ... N, and collects all the results in a list,
which it returns.
[*] Where I say "function" in any of this, I should really be saying
"callable". If you don't know what this means, ignore it. Not
important.

In this case, the first argument of "map" is "apply". "apply" is a
builtin which takes a function as its first argument, and applies it
to whatever is in the sequence that is its second argument. (It also
has the ability to deal with keyword args, but forget those for now.)

Take in for granted, for now, that "args" in "[args]*len(fns)" refers
to an empty list. In which case "[args]*len(fns)" creates a list of
empty lists, whose length is equal to the length of the sequence
"fns".

Take it for granted, for now, that "fns" is a sequence of
functions. So, "map(apply, fns, [args]*len(fns))" calls the first
function in the "fns" (giving it no arguments) and remembers the
result, then it calls the next function in "fns" (with no args), and
so on ... then it returns all the results.

Now, "lambda" is a way of creating anonymous functions. You could
created "apply_each" like this:

def apply_each(fns, args=[]):
return map(apply, fns, [args]*len(fns))

So, "apply_each" is basically a function, which takes a sequence of
functions, calls them all, and returns all the results in a list.

Let's define a few simple functions, which take no arguments and
return something ... and pass them to apply_each
apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))
def one(): return "one" .... def two(): return "two" .... def three(): return "three" .... apply_each([one,two,three]) ['one', 'two', 'three']


Now, you could pass a second argument to "apply_args", in which case
it should be a sequence, and the elements of the sequence would be
used as the arguments in each of the calls to the functions you pass.
Without seeing the broader context in which the original appears, it's
difficult to say why it was defined the way it was. Nowadays, I
suspect that many Pythoneers would prefer something like

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

- The stuff appearing after "return" is a List Comprehension.

- The "*" in "fn(*args") means "args is a sequence: pass its contents as
separate arguments to 'fn'".
Jul 18 '05 #3
Senthoorkumaran Punniamoorthy wrote:
I found this code in the book text processing with Python. But having
little difficulty understanding how exactly it works and what should
be passed as argument? Can someone decode this for me please?

apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))


def apply_each (fns, args = []):
return map (apply, fns, [args]*len(fns))

map(...)
map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list
of
the items of the sequence (or a list of tuples if more than one
sequence).

so apply_each ([func1, func2, func3], [arg1, arg2])
is the same as
[func1 (arg1, arg2), func2 (arg1, arg2), func3 (arg1, arg)]

or using list comprehension
[func (arg1, arg2) for func in [func1, func2, func3])

Daniel

Jul 18 '05 #4
Thanks all of you explained me. Now I understand more clearly than every before.

Regards
Senthoor

"Daniel Dittmar" <da************@sap.com> wrote in message news:<c4**********@news1.wdf.sap-ag.de>...
Senthoorkumaran Punniamoorthy wrote:
I found this code in the book text processing with Python. But having
little difficulty understanding how exactly it works and what should
be passed as argument? Can someone decode this for me please?

apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))


def apply_each (fns, args = []):
return map (apply, fns, [args]*len(fns))

map(...)
map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list
of
the items of the sequence (or a list of tuples if more than one
sequence).

so apply_each ([func1, func2, func3], [arg1, arg2])
is the same as
[func1 (arg1, arg2), func2 (arg1, arg2), func3 (arg1, arg)]

or using list comprehension
[func (arg1, arg2) for func in [func1, func2, func3])

Daniel

Jul 18 '05 #5

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

Similar topics

14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
3
by: MarcJessome | last post by:
Hi, I was wondering if someone could help me through learning C++. I've tried learning before, but I find I would work better if I had someone that could help explain a few things to me. Im using...
20
by: nicolas.riesch | last post by:
I try to understand strict aliasing rules that are in the C Standard. As gcc applies these rules by default, I just want to be sure to understand fully this issue. For questions (1), (2) and...
1
by: Iwan Petrow | last post by:
Hi, I want to create a short-cut for a button (for example F2, F3..) without menu. Is there a direct way or I have to write key down event (with this I have to add this event for each control in...
3
by: Guy Penfold | last post by:
Hi all, I have an asp.net application exhibiting rather alarming behaviour. Among other things, the application provides actors with an online profile that allows them to showcase their skills,...
6
by: Raymond Lewallen | last post by:
I understand the differences between And and AndAlso and the differences between Or and OrElse. Under what circumstance would you NOT use short-circuiting when evaluating expressions? It seems to...
1
by: freegnu | last post by:
hi all, i want to modify a class's interface, that will reference many classes which inherit it is there any advice to me? thanks
2
by: CptDondo | last post by:
I'm trying to write what should be a simple program, and it keeps hanging if I use volatile.... The program, stripped of its error checking, is this: unsigned short * start; unsigned short *...
2
by: varois83 | last post by:
Hi I am just starting with PHP. I am reading a PHP 5 book and they say to not use short tags and to actually disable them so I see an error message if I don't form my tags correctly....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.