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

Mikhail V mikhailwas at gmail.com
Sun Apr 29 16:23:40 EDT 2018


On Sun, Apr 29, 2018 at 7:22 PM, Mikhail V <mikhailwas at gmail.com> wrote:
> On Sun, Apr 29, 2018 at 3:30 AM, Tim Peters <tim.peters at gmail.com> wrote:
>
>> Time to note another subtlety:  people don't _really_ want "a new
>> scope" in Python.  If they did, then _every_ name appearing in a

> If there is demand for this, how about just introducing a
> derived syntax for the "auto-called" def block, say, just "def" without a name:
>
> def :
>     global a
>     x = 1; y = 2
>     a = x + y
> print (a)
>

Or even better, it would be better to avoid overloading "global" or "return",
and use dedicated prefix for variables that are pushed to outer scope.
I think it would look way better for cases with multiple variables:

def func():
    state = 0
    def:
        localstate1 = state + 1
        localstate2 = state + 2
        & localstate1
        & localstate2
    print (localstate1)
    print (localstate2)

Prefix in assignment would allow more expressive dispatching:

def func():
    state = 0
    def:
        localstate1 = state + 1
        localstate2 = state + 2
        & M = state + 3
        & L1,  & L2 = localstate1, localstate2
    print (L1, L2, M)


Imo such syntax is closest do "def" block and should be so,
because functions have very strong association with new scope
definition, and same rules should work here.
Not only it is more readable than "with local()", but also
makes it easier to edit,  comment/uncomment lines.


More information about the Python-ideas mailing list