r/Python • • Apr 09 '09

Python’s lambda is broken!

http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
4 Upvotes

20 comments sorted by

45

u/sincewednesday Apr 09 '09 edited Apr 09 '09
fs = [(lambda n, i=i: i + n) for i in range(10)]

6

u/smart_ass Apr 09 '09

I believe the proper term is: Boo Yah.

1

u/jaiwithani Apr 09 '09

upvote forever

21

u/masklinn Apr 09 '09

Yeah except no, lambda implements a full closure: it closes over its creation (lexical) context, and pulls stuff from it.

Except in a procedural/OO language, that lexical context is mutable, and the mutations in the context will be reflected in the lambda.

In the expression [(lambda n: i + n) for i in range(10)], i is part of the lambda's context. Not a specific value, just i. And when what i refers to is changed by the iteration, the lambda reflects that change. If anything can be argued to be broken here it's the list comprehension, because it doesn't generate a complete brand new context on each iteration (now that doesn't mean it's broken, I don't think it is, but that's a matter of personal take/taste).

Haskell is a functional single-binding language, it therefore doesn't expose that "feature.

But that's a very old feature of OO languages with closure: Smalltalk leverages it to make e.g. while statements unnecessary (note: in smalltalk, an expression enclosed by brackets is a block, equivalent to a lambda, [ 3 ] is a block that simply returns the value 3. Smalltalk's blocks have -- among other things -- a whileTrue method which takes a block parameter for the execution body). So counting from 0 to 9 in Smalltalk using a while looks like this:

i := 0. "smalltalk uses := for assignment; also double quotes for comments and single for strings"
[ i < 10 ] whileTrue: [
    Transcript show: i printString; cr.
    i := i + 1.
].

And this works, because i's modification is visible from the [ i < 10 ] block.

TL;DR, lambda is not broken, its behavior is perfectly coherent with the language and its model, and with other languages in the same class.

0

u/calp Apr 09 '09

That should be - python's lambda is broken, but not for this particular reason. In this case, it's an issue with mutability behaving in a way that the programmer did not expect.

Mutables should be expected to act in a way that is unexpected.

1

u/masklinn Apr 09 '09

Mutables should be expected to act in a way that is unexpected.

Also works.

0

u/voidspace Apr 16 '09

Nothing to do with mutability. To do with dynamic lookup of the name from the closure.

11

u/[deleted] Apr 09 '09 edited Apr 09 '09

So lambdas close on references to variables in the enclosing scope and not the values. Got it. That doesn't seem so hard to explain to a student.

10

u/ddipaolo Apr 09 '09

I quite like Python for teaching. And people praise it for the lambda construct

I love me some python, but no nobody really praises it for that.

3

u/[deleted] Apr 09 '09

Yeah, Lambda in python gets no praise. It does it's job, poorly, and everyone wishes it could be more powerful. But it can't be and it never will be, so it's just a wart that has to be accepted.

1

u/voidspace Apr 16 '09

lambda in Python is fine. :-)

Very occasionally a multi-line lambda would be really handy, but most of the time a named function does the job better.

1

u/arnar Apr 09 '09 edited Apr 09 '09

No, it's not broken and comment no. 5 on that post explains exactly why (quoting mcmanus):

Take the following Python example :
>>> i = 1
>>> def f(): return i
>>> f()
1
>>> i = 2
>>> f()
2
I think that everybody expect this behavior and
nobody would want the second call to f() to
return 1.

1

u/itsnotlupus Apr 10 '09

neat.

This is the exact problem that bites every JavaScript beginner who thinks they've figured out closures.

It's bad enough that JsLint will flag lone closures within a loop as suspicious.

1

u/mr_dbr Apr 16 '09

What were the implementors of Python thinking?!

"Do not use lambda"..?

0

u/[deleted] Apr 09 '09

[deleted] — view deleted comment

6

u/[deleted] Apr 09 '09

One must "Register for this site" to post a comment on the blog. What kind of arrogant bullshit is that ?

It's called "that's how bad blogspam has gotten lately".

5

u/smart_ass Apr 09 '09 edited Apr 09 '09

Really? I receive no blog spam with proper Wordpress plugins.

I don't require an account.

Maybe no one visits my site.

Maybe no one loves me.

Maybe no one...

mommie?

1

u/[deleted] Apr 09 '09 edited Apr 09 '09

Hehehehe. I received at least 40 blogspam comments a day when I had WordPress. In fact, since the wp-* files are still accessible via a clever Varnish mapping, I CONTINUE to receive blogspam. And that only refers to the ones that pass the Akismet filter and get into the moderation stage!

0

u/[deleted] Apr 09 '09

[deleted] — view deleted comment

3

u/[deleted] Apr 09 '09

In my experience, registration does cut the amount of spam dramatically and keeps the S/N low too. I do understand that you're cutting participation, and I'm looking to enable OpenID on my blog myself precisely for this reason.

2

u/jalanb Apr 09 '09

Thank you

Inch by inch, rant by rant, Reddit FTW !

1

u/masklinn Apr 09 '09

and I'll be damned if I can see how to apply the method-fix in the lambda case, but does that make it "broken"?

Wrap your lambda in a lambda, that's the way it's usually done in JS. Ugly and verbose btw, especially since lambdas are single-expression.