
On Mon, 5 Dec 2022 at 06:24, David Mertz, Ph.D. <david.mertz@gmail.com> wrote:
On Sun, Dec 4, 2022, 2:08 PM Chris Angelico <rosuav@gmail.com> wrote:
You're not the first to try to use globals() for this, but it means that the context manager works ONLY at top-level.
True. I know that. But if you're inside a function, you already have scope containment, so the issue feels like "so what?"
Sure. Notably, though, it also only works at the exact SAME top-level that it was defined at, unless you mess with introspection. And, of course, it still can't shadow variables, which really makes the whole concept of scoping rather meaningless - like using "var" deep inside a JavaScript function.
But doh! I always forget about which way the locals() / globals() mutability thing goes. A regular del without exec is better, of course.
Fair enough! I think the Py2 exec statement was a bit different, with some weird abilities to mutate the local scope, but in Py3 it's a lot easier to reason about - it just takes whatever dictionary you pass it, and mutates that. So it actually wouldn't make a difference to the mutability question - I think. There might be execptions, I mean exceptions. ChrisA