[Python-ideas] PEP 572: Statement-Local Name Bindings
Robert Vanden Eynde
robertve92 at gmail.com
Wed Feb 28 04:04:13 EST 2018
True, but it's also extremely wordy. Your two proposed syntaxes, if I
have this correct, are:
1) '(' 'with' EXPR 'as' NAME ':' EXPR ')'
2) '(' EXPR 'with' EXPR 'as' NAME ')'
Of the two, I prefer the first, as the second has the same problem as
the if/else expression: execution is middle-first. It also offers only
a slight advantage (in a comprehension) over just slapping another
'for' clause at the end of the expression. The first one is most
comparable to the lambda helper example, and is (IMO) still very
wordy. Perhaps it can be added in a section of "alternative syntax
forms"?
Considering the 3rd syntax :
'(' EXPR 'with' NAME '=' EXPR ')'
Wouldn't have the problem of "execution being middle first and would
clearly differenciate the "with NAME = CONTEXT" from the "with CONTEXT as
NAME:" statement.
Considering the PEP :
1) I think I spoke too fast for SqlAlchemy using "where", after looking up,
they use "filter" (I was pretty sure I read it somewhere...)
2) talking about the implementation of thektulu in the "where =" part.
3) "C problem that an equals sign in an expression can now create a name
binding, rather than performing a comparison." The "=" does variable
assignement already, and there is no grammar problem of "=" vs "==" because
the "with" keyword is used in the expression, therefore "with a == ..." is
a SyntaxError whereas "where a = ..." is alright (See grammar in thektulu
implemention of "where").
Remember that the lexer knows the difference between "=" and "==", so those
two are clearly different tokens.
4) Would the syntax be allowed after the "for" in a list comprehension ?
[[y, y] for x in range(5) with y = x+1]
This is exactly the same as "for y in [ x+1 ]", allowing the syntax here
would allow adding "if" to filter in the list comp using the new Variable.
[[y, y] for x in range(5) with y = x+1 if y % 2 == 0]
5) Any expression vs "post for" only
When I say "any expression" I mean:
print(y+2 with y = x+1)
When I say "post for in list comp" I mean the previous paragraph:
[y+2 for x in range(5) with y = x+1]
Allowing both cases leads to having two ways in the simple case [(y,y) with
y = x+1 for x in range(5)] vs [(y,y) for x in range(5) with y = x+1] (but
that's alright)
Allowing "any expression" would result in having two ways to have variable
assignement :
y = x + 1
print(y+2)
Vs:
print(y+2 with y = x+1)
One could argue the first is imperative programming whereas the second is
Functional programming.
The second case will have to have "y" being a Local variable as the new
Variable in list comp are not in the outside scope.
6) with your syntax, how does the simple case work (y+2 with y = x+1) ?
Would you write ((x+1 as y) + 2) ? That's very unclear where the variable
are defined, in the [(x+1 as y), y] case, the scoping would suggest the "y"
Variable is defined between the parenthesis whereas [x+1 as y, y] is not
symmetric.
The issue is not only about reusing variable.
7) the "lambda example", the "v" variable can be renamed "y" to be
consistent with the other examples.
8) there are two ways of using a lamba, either positional args, either
keyword arguments, writing
(lambda y: [y, y])(x+1)
Vs
(lambda y: [y, y])(y=x+1)
In the second example, the y = x+1 is explicit.
9) the issue is not only about reusing variable, but also readability,
otherwise, why would we create Tempory variables we only use once ?
10) Chaining, in the case of the "with =", in thektulu, parenthesis were
mandatory:
print((z+3 with z = y+2) with y = x+2)
What happens when the parenthesis are dropped ?
print(z+3 with y = x+2 with z = y+2)
Vs
print(z+3 with y = x+2 with z = y+2)
I prefer the first one be cause it's in the same order as the "post for"
[z + 3 for y in [ x+2 ] for z in [ y+2 ]]
11) Scoping, in the case of the "with =" syntax, I think the parenthesis
introduce a scope :
print(y + (y+1 where y = 2))
Would raise a SyntaxError, it's probably better for the variable beeing
local and not in the current function (that would be a mess).
Remember that in list comp, the variable is not leaked :
x = 5
stuff = [y+2 for y in [x+1]
print(y) # SyntaxError
Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180228/d9c9a06f/attachment.html>
More information about the Python-ideas
mailing list