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

Mikhail V mikhailwas at gmail.com
Sun Apr 29 12:22:52 EDT 2018


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
> binding context (assignment statement target, `for` target, ...) for
> the duration would vanish when the new scope ended.  What they really
> want is a new scope with an implied "nonlocal" declaration for every
> name appearing in a binding context _except_ for the specific names
> they're effectively trying to declare as being "sublocal" instead.
> """
>
> If by "new name" you mean that `x` didn't appear in any earlier line,
> then Python's current analysis would classify `x` as local to the
> current function (or as global if this is module-level code ...).
> That wouldn't change.


I have hard time understanding what is
the demand here actually. (it's been too many
posts and ideas to absorb)

>From your description of "what people need" -
how is this different from current "def"?
Now I can use nested "def"s and call it right away:

def d():
    global a
    x = 1; y = 2
    a = x + y
d()
print (a)

And this will do the thing: here if the "a" variable is "new" then it
will be initialized and pushed to the outer scope, right?
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)

Which would act just like a scope block without any new rules introduced.
And for inline usage directly inside single-line expression - I don't
think it is
plausible to come up with very nice syntax anyway, and I bet at best you'll
end up with something looking C-ish, e.g.:

if (def {x=1; y=2; &a = x + y } ) :
    ...

As a short-cut for the above multi-line scope block.



Mikhail


More information about the Python-ideas mailing list