4 ms·
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 func
by _sdegutis 9y ago
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.
- e12e 9y agoWhy 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.
- benji-york 9y agoYou might enjoy Coconut (http://coconut-lang.org/ http://coconut-lang.org/).
- developer2 9y agoPython 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.
- joshuamorton 9y agoHow 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?