[Python-ideas] A "local" pseudo-function

Ethan Furman ethan at stoneleaf.us
Sun Apr 29 15:39:34 EDT 2018


On 04/28/2018 10:16 AM, Tim Peters wrote:

> ... but do realize that since PEP 572 dropped any
> notion of sublocal scopes, that recurring issue remains wholly
> unaddressed regardless.

If we need a sublocal scope, I think the most Pythonic* route to have it would be:

     with sublocal():
         blah blah

which would act just like local/global does now:

   - any assignment creates a new variable
     - unless that variable has been declared global/nonlocal
   - plain reads (no assignment ever happens) refer to nonlocal/global/built-in
     names

This has the advantages of:

  - no confusion about which variables are sublocal (acts like a new function scope)
  - no extra parens, assignments, expressions on "with sublocal():" line

Possible enhancements:

  - give sublocal block a name "with sublocal() as blahblah:" and then reuse that block
    again later ("with blahblah:") or maybe pass it to other functions...

Of course, as previously stated, this is orthogonal to PEP 572.

--
~Ethan~


More information about the Python-ideas mailing list