There was at least one recent thread proposing this same idea: A "with" variant for use in expressions . I feel like that thread kind of derailed, though, after not getting a lot of support.
You’re certainly right that there’s not a big difference between those examples and the way they would be written with the proposed syntax, but I don’t think those examples illustrate places where this change would have its impact. In the thread I linked above, the proposal was similarly dismissed (by someone else) as being “code golf,” but to me this isn’t really about syntax, but rather about making context managers available in expressions.
I’m generally in favor of making more structures available in expressions, mostly because I tend to write code with a lot of comprehensions. If I’m writing something and decide that, for example, I want to do a json.load inside of a comprehension[1], my natural tendency is going to be just to use open(filename) and assume that things will get cleaned up properly, rather than unrolling things into a loop so I can use the context manager, or adding an import so I can use the pathlib stuff. I think it would be nice to have the “safe” version available as an expression, in the same way that we have lambdas and if expressions and walruses and all that good stuff.
So my reaction hasn’t changed since the last thread: I’ve wanted this feature at multiple points in the past, and I’m pretty sure I would use it regularly if it were added.
Maybe this is a bad idea™, but it’s still something I do all the time, looping over structured files and extracting a small piece of information from each into some data structure or another. ↩︎