Unintelligible Lambda Tricks

chajadan python at chajadan.net
Thu Feb 21 22:44:34 EST 2002


data.__setitem__(4,42)

def swap(a,x,y):
a[x] = (a[x], a[y])
a[y] = a[x][0]
a[x] = a[x][1]
You can write this as a lambda expression like this:

>swap = lambda a,x,y:(lambda
>f=a.__setitem__:(f(x,(a[x],a[y])),f(y,a[x][0]),f(x,a[x][1])))()


I'm going to indulge in a question that is preceeding research...if I'm 
spanked and told to rtfm then I will do so, but I figured with the zealous 
attraction some have to lambdas, someone would be more than happy to give 
me a quick run through.

I don't understand why __setitem__ takes 2 arguments, but in the lambda () 
there are 3...

I'm also not sure how it gets its values for a, x, or y---are these pure 
abstract variable names, or do the refer to previously defined values for 
variables with the same name?

Basically, I have no freakin' clue how python (or any other language) runs 
through that lambda. I don't understand the sequence of events. What does 
it evaluate, where does it pull the values from, if f is defining a 
function, why is f used ~in~ the function... AAarrgh!

I guess I better go read...

--chajadan





More information about the Python-list mailing list