[mdione at grulic.org.ar: modifying locals]

Chris Angelico rosuav at gmail.com
Mon Oct 12 04:50:40 EDT 2015


On Mon, Oct 12, 2015 at 6:27 PM, Marcos Dione <mdione at grulic.org.ar> wrote:
>     So far I managed to do the first part, but not the second. I managed
> to transfer the locals back from the remote. My problem is modifying the
> locals in remote()'s __exit__() method. As I'm running the code using
> exec(), it's not just a matter of modifying locals()[2].

Short answer: You can't.

Modifying locals is not something that's ever supported (except in the
trivial case where locals() is globals(), as mutating globals() *is*
supported). Personally, I'd be inclined to switch from a 'with' block
to a function with a decorator - something like this:

[local code 1]
@remote(...)
def operationname():
    [remote code]

[local code 2]


The decorator could then do the actual remote call. I'm assuming here
that it's a synchronous call, and that [local code 2] doesn't begin
until [remote code] has finished. Then you simply use nonlocal and/or
global to mark which names need to get rebound after the call. This
might even be possible with existing CPython - I'm not sure.

ChrisA



More information about the Python-list mailing list