Hacker News new | comments | show | ask | jobs | submit login

Technically python doesn't have closures, which is what he's talking about here. Python has lambdas, which are a sort of crippled version of anonymous functions.


EDIT: of course the replies are correct. i suppose it was unclear what the parent meant by "anonymous functions". if he meant lambdas, then they are indeed closures but without the same capabilities as normal python functions. their capabilities are unrelated to the fact that they're anonymous or closures.

None of what you just said is particularly true.

Python has closures.

Lambdas are anonymous functions which support only expressions, like all functions they can form closures.

Named functions also form closures and can contain any number of statements or expressions.

Technically Python has what I like to call "Java closures" (although I admit that is not really fair). In Python the body of an anonymous function (lambda) can only be an expression and the bindings closed over by a named inner function are immutable. You can get around the second limitation with a single element list and this is the same way you get around Java's limitation that a local variable referenced by an anonymous inner class be final.

The single element list is no longer necessary w/ Python 3's nonlocal keyword (along w/ the old global keyword)

Python has full-featured closures (they must be named functions). But like you said, lambda is the neutered, anonymous version of these.

Python 3 has full featured closures. In Python 2 it is not possible to alter what a variable references. It's still possible to alter the contents of the object being referenced leading to the "put the value in a list" type of hackery to simulate full featured closures.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact