Re:Python 1.0.0 is out (1994)
by _sdegutis on Saturday January 27, 2018 @09:30AM
(#16246334)
As a minor nitpick it bugs me a lot that there are no truly anonymous functions. You either have to use a lambda which is limited to expressions, or name a function and use it as a first class object. Every other language in the same domain has this feature.
[ Parent
| on HN ]
Why does it matter?
I suppose you could avoid the possibility of a name clash if the interpreter maintained a hidden, name-mangled reference for you...?
Personally I think it's a bigger problem that python isn't all that well suited to functional programming because of lack of tco etc (see slackness python etc). But I understand the design decision.
Re:Python 1.0.0 is out (1994)
by developer2 on Saturday January 27, 2018 @11:25AM
(#16246764)
Python actually does have a kind of "anonymous function" feature, at least in a way that replaces most common use cases. It just happens to be bundled in a unique Python-like way. A lot of people don't seem to know about @contextmanager (contextlib.contextmanager) - it's what provides the functionality for "with" blocks. You've probably seen with() used with file operations (open file, run block of code, file is closed automatically at end of block), but I suspect many developers are unaware that you can create your own context managers.
While they are not true anonymous functions, I have yet to stumble on a scenario where a context manager did not fit the bill. They're easy to create - nothing more than a try/except/finally with a "yield" call to execute... you guessed it, essentially an anonymous function block. It's a little mind-warping the first time through, but it winds up creating clean code. PEP 343 is a good source to learn more.
Re:Python 1.0.0 is out (1994)
by joshuamorton on Saturday January 27, 2018 @06:04PM
(#16248614)
How do contextmanagers (which by the way, @contextmanager is sugar for manually defining an object with __enter__ and __exit__ methods) take the place of anonymous functions?
Re:Python 1.0.0 is out (1994)
by benji-york on Saturday January 27, 2018 @10:18AM
(#16246486)
You might enjoy Coconut (http://coconut-lang.org/).