Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the locals dict?

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.

like image 204
MessingWithPython Avatar asked Nov 08 '22 23:11

MessingWithPython


1 Answers

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()
like image 171
J_H Avatar answered Nov 14 '22 22:11

J_H