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?
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.