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

Tim Peters tim.peters at gmail.com
Sat Apr 28 16:40:35 EDT 2018


[Ed Kellett <e+python-ideas at kellett.im>]
> How about if you just can't have an expression in a local()?

See the quadratic equation example in the original post.  When working
with expressions, the entire point of the construct is to define
(sub)local names for use in a result expression.


> There are a few obvious alternative possibilities:
>
> 1. No special case at all:
>
>     local(x=1, y=2, _=x+y)

As above.


> 2. Co-opt a keyword after local():
>
>     local(x=1, y=2) in x+y

Requiring "in" requires annoying syntactic repetition in the common

    if local(match=re.match(regexp, line)) in match:

kinds of cases.  Making "in" optional instead was discussed near the
end of the original post.  I agree that your spelling just above is
more obvious than `local(x=1, y=2, x+y)` which is why the original
post discussed making an "in clause" optional.  But, overwhelmingly,
it appears that people are more interested in establishing sublocal
scopes in `if` and `while` constructs than in standalone expressions,
so I picked a spelling that's _most_ convenient for the latter's
common "just name a result and test its truthiness" uses.


> 3. Co-opt a keyword inside local():
>
>     local(x=1, y=2, return x+y)

Why would that be better than local(x=1, y=2, x+y)?  That no binding
is intended for `x+y` is already obvious in the latter.


> I hate the first and wish the _ pattern would die in all its forms, but
> it's worth mentioning. I don't think there's much to choose between the
> other two, but 2 uses syntax that might have been valid and meant
> something else, so 3 is probably less confusing.

Indeed, 3 is the only one I'd consider, but I don't see that it's a
real improvement.  It seems to require extra typing every time just to
avoid learning "and it returns the value of the last expression" once.


More information about the Python-ideas mailing list