Basically instead of this:
context1.a
context1.b()
context2.a
context2.b()
I want to write:
with context1():
a
b()
with context2():
a
b()
Ideally I would do this by switching out the "locals dictionary" with an object with a custom __get__
, but I'm not aware of how to do that or if it's even possible.
Why?: I've implemented a kind of predicate dispatching (for fun), but I can only use by explicitly naming the context each time: context.a() + context.b()
is annoying to write all the time.
Don't do that. Just because a Turing machine can do something doesn't mean that's a good way to communicate your intent to folks.
Simply stick to standard syntax, and then your code will be clear to other engineers.
with context1() as c:
c.a
c.b()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With