[Tutor] (OT) Flame wars

Luke Paireepinart rabidpoobear at gmail.com
Tue Nov 7 00:04:12 CET 2006


Chris Hengge wrote:
> I may have just missed the point to your attempt to derail this 
> conversation =P
Ah, well, don't worry.  I didn't learn of lambda until I'd been using 
Python for a year or more.
I was trying to pass arguments to callbacks in TKinter.
one of the Pythonistas (Alan, Danny, Kent) told me "Oh, that's trivial 
with lambda"
and since then, I've tried to read up on lambda and Functional 
Programming in general.
> However..
>
> Why do all that when you can just
>
> str = "Hello World"
> print str * 2
>
> (Maybe I missed some concept that this small example doesn't accuratly 
> reflect)
Yes, I do believe you're correct here, Chris :)
I will not presume to know enough about lambda to explain it to you,
but I will refer you to these articles:
http://www.secnetix.de/~olli/Python/lambda_functions.hawk
and Alan Gauld's tutorial, of course :)
specifically, the page
http://www.freenetpages.co.uk/hp/alan.gauld/tutfctnl.htm

Or browse there from the main page
http://www.freenetpages.co.uk/hp/alan.gauld/
click 'functional programming'  under Advanced Topics'
if you want the menu frame to remain on the left of the screen.


Consider this:
You don't say
outputstring = "Hello, World!"
print outputstring

because you're only using the string once, and it's not something that 
would look ugly all on one line.
Instead, you do
print "Hello, World!"


An alternate example:
a_list = [[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a],
[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a],
[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a],
[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a]]
print a_list

would make more sense than

print [[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a],
[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a],
[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a],
[a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b,a]]

because then if you need to change the list, or use it again somewhere,
you don't have to go through the pain of typing it out each time,
since in the first case, you assigned it to a variable, a_list.


The long and short of it is: (or more specifically, the short of it)
 lambda lets you define functions you don't have to bind to a name.
so, supposing you have the function
def call_a_function(a_function):
    a_function()

That will call a function object,
you can do something like this:
def f():
    sys.stdout.write("Hello, World!")
call_a_function(f)

or you could do
call_a_function(lambda: sys.stdout.write("Hello, World!"))

It's like the string example above.  It's a waste to have the function 
definition on two lines,
and the call on another line, if the function's compact enough to fit on 
a single line using lambda.
But also note that you can't reuse the lambda function unless you store 
it in a variable.
In this way, it's similar to the 'print "Hello, World!"' statement.



I hope that helps you, Chris.
-Luke


More information about the Tutor mailing list