<div dir="ltr">This doesn't address the fact no one actually needs it.  But if we WANTED a sublocal() context manager, we could spell it something like this:<br><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra"><font face="monospace, monospace">In [42]: @contextmanager</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...: def sublocal(**kws):</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     _locals = locals().copy()</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     _globals = globals().copy()</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     for k, v in kws.items():</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:         if k in locals():</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:             exec(f"locals()['{k}'] = {v}")</font></div><div class="gmail_extra"><span style="font-family:monospace,monospace">    ...:         elif k in globals():</span><br></div><div class="gmail_extra"><font face="monospace, monospace">    ...:             exec(f"globals()['{k}'] = {v}")</font></div><div class="gmail_extra"><span style="font-family:monospace,monospace">    ...:     yield</span><br></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     locals().update(_locals)</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     globals().update(_globals)</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:</font></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">In [43]: a = 42</font></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">In [44]: with sublocal(a=43):</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     showa()</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:</font></div><div class="gmail_extra"><span style="font-family:monospace,monospace">43</span><br></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">In [45]: showa()</font></div><div class="gmail_extra"><font face="monospace, monospace">42</font></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">In [46]: with sublocal():</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     a = 41</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:     showa()</font></div><div class="gmail_extra"><font face="monospace, monospace">    ...:</font></div><div class="gmail_extra"><font face="monospace, monospace">41</font></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">In [47]: showa()</font></div><div class="gmail_extra"><font face="monospace, monospace">42</font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Apr 29, 2018 at 4:20 PM, Tim Peters <span dir="ltr"><<a href="mailto:tim.peters@gmail.com" target="_blank">tim.peters@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">[Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>>]<br>
<span class="gmail-">> If we need a sublocal scope, I think the most Pythonic* route to have it<br>
> would be:<br>
><br>
>     with sublocal():<br>
>         blah blah<br>
><br></span><br>
As covered most recently in an exchange with Tim Delaney, best I can<br>
tell absolutely nobody has wanted that.  By "sublocal scope" they<br>
don't mean a full-fledged new scope at all, but a kind of limited<br>
"shadowing" of a handful of specific, explicitly given names.  It acts<br>
like a context manager, if there were a way to clearly spell<br></blockquote></div><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>