Is it possible to change the locals dict?

Basically instead:

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 selecting the "locals dictionary" with an object with a custom __get__ , but I don't know how to do this or if it's possible.

Why ?: I implemented a kind of predicate dispatch (for fun), but I can only use explicitly naming the context every time: context.a() + context.b() annoying to write all the time.

+5
source share
1 answer

Do not do this. Just because a Turing machine can do something does not mean that it is a good way to communicate your intent to people.

Just stick with the standard syntax and then your code will be clear to other engineers.

  with context1() as c: ca cb() 
0
source

Source: https://habr.com/ru/post/1266684/


All Articles