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

Yury Selivanov yselivanov.ml at gmail.com
Sat Apr 28 03:16:40 EDT 2018


On Sat, Apr 28, 2018 at 2:07 AM Tim Peters <tim.peters at gmail.com> wrote:
[...]
> Speaking of which, "sublocal" would be a more accurate name, and less
> likely to conflict with existing code names, but after people got over
> the shock to their sense of purity, they'd appreciate typing the
> shorter "local" instead ;-)

> For that matter, I'd be fine too with shortening it to "let".  In
> fact, I prefer that!  Thanks :-)

Great! :)

[...]
> So, curiously enough, I'd be fonder of

>      result_expression "where" name=expression, ...

> than of

>      "let" name=expression, ...

My gripe about the "where"-like syntax family (including "as", proposed by
many) is that the whole expression needs to be read backwards to make sense
of `name*. It's one of the things that annoy me in languages like SQL,
where the relevant "AS" keyword can be buried a couple of screens
below/above the area of interest.  That's why I find the "let" form
superior.

[..]
> instead.  In _an expression_, I naturally group the

>      a = 3, a

> part as the unintended

>      a = (3, a)

> When I'm thinking of function calls, though, I naturally use the intended

>      (a=3), a

> grouping.  I really don't want to fight with what "everyone already
> knows", but build on that to the extent possible.

Looking at all of these and other examples in your email I have to agree
that the version with parenthesis reads way more clearly. A different (and
correct in this case) precedence of "," between parens is pretty much
hardwired in our brains.

[..]
> Why not?  A great many objects in Python are _designed_ so that their
> __bool__ method does a useful thing in a plain

>       if object:

> test.  In these common cases, needing to type

>       if let name=object, object:

Alright. And yes, I agree, surrounding parens are badly needed. Maybe we
can replace parens with {}? Although my immediate reaction to that is "it's
too ugly to be taken seriously".

In any case, the similarity of this new syntax to a function call still
bothers me.  If I just looked at some Python 3.xx code and saw the new
"let(..)" syntax, I would assume that the names it declares are only
visible *within* the parens. Parens imply some locality of whatever is
happening between them.  Even if I googled the docs first to learn some
basics about "let", when I saw "if let(name...)" it wouldn't be immediately
apparent to me that `name` is set only for its "if" block (contrary to "if
let a = 1: print(a)").

Yury


More information about the Python-ideas mailing list