Hacker Newsnew | comments | ask | jobs | submitlogin
endlessvoid94 834 days ago | link | parent

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.

kingkilr 834 days ago | link

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.


spacemanaki 834 days ago | link

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.


narm60 833 days ago | link

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


oinksoft 834 days ago | link

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


hetman 833 days ago | link

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.


Lists | RSS | Bookmarklet | Guidelines | FAQ | DMCA | News News | Feature Requests | Bugs | Y Combinator | Apply | Library