News for hackers. Stuff that matters.
Re:Python’s lambda is broken by yummyfajitas on Thursday April 09, 2009 @02:21PM (#554861)
Yes, Python closes over references to a mutable variable, and list comprehensions work the same way. It doesn't close over the value.

I'd make the suggestion that closures are not very "Pythonic". Use objects instead:

    class add_n_func:
        def __init__(self, n):
            self.n = n
        def __call__(self, x):
            return x+self.n

    return [add_n_func(n) for n in range(10)]
Re:Python’s lambda is broken by ii on Thursday April 09, 2009 @06:02PM (#555252)
I would suggest that the most "Pythonic" way would be to use functools.partial and operator.add from the standard library.

As I already wrote it would look "fs = [partial(add, i) for i in range(10)]" and it works as expected.

Re:Python’s lambda is broken by pavelludiq on Thursday April 09, 2009 @02:44PM (#554900)
How is a 5 line class with 2 underscore methods more pythonic than a nested lambda?
Re:Python’s lambda is broken by yummyfajitas on Thursday April 09, 2009 @03:04PM (#554944)
While python supports some basic functional programming, it's primarily a procedural/object oriented language. And it shows.

As the article demonstrates, using nested lambda's doesn't work the way you expect for closures. Using an object does.

edit: update. I found the presentation where guido talks about how he dislikes all the functional stuff, including lambda.

http://74.125.93.104/search?q=cache:2QztD1KncJgJ:www.python....

Re:Python’s lambda is broken by tptacek on Thursday April 09, 2009 @03:49PM (#555008)
Does it make you sad that this is the same answer you'd have given if the language in question was Java?

Just rubbing it in.