5 ms·
Interesting, I think that would make sense for Python. def do_thing(): with open('foo', 'r') as f with a_lock f.read() frob
by toxik 6y ago
Interesting, I think that would make sense for Python.
def do_thing():
with open('foo', 'r') as f
with a_lock
f.read()
frobnicate()
I guess the issue is that there is less of a clear idea of scope in Python, what does the above construct do at a module level? What does it do inside another with block? A lot of special rules that don't conveniently fit into something else unlike what I suppose C# has.
- Asooka 6y agoYou can get that syntax right now using ExitStack.
- toxik 6y agoYou can get something in that general area yes. This is actually vaguely reminiscent of auto release pools in Cocoa.
- nurbl 6y agoWouldn't it be more natural to do this sort of thing with decorators?
- Groxx 6y agoFor trivial examples, sure. And there are a lot of those in practice, so it definitely applies sometimes. But decorators aren't really usable when you have any intermediate steps that need to be done, like def do_thing(): with a_lock check_f_cache_while_locked() # like this with open('foo', 'r') as f populate_cache(f)