Context manager able to write to the caller's namespace
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Jan 19 00:56:41 EST 2018
On Fri, 19 Jan 2018 16:49:49 +1100, Chris Angelico wrote:
[...]
> 1) Context manager was called from global scope, and needs access to
> globals() or locals() as returned in the caller
Ayyyyeeee! /facepalm
Of course the caller can just pass locals() to the context manager. Why
didn't I think of that?
I think I'm okay with that as a solution, but...
>>>> def mutate():
> ... locals = sys._getframe(1).f_locals ... locals["x"] = 4
> ...
I'll play around with that too. Just for kicks :-)
--
Steve
More information about the Python-list
mailing list