[Python-ideas] [Python-Dev] Language proposal: variable assignment in functional context
python at lucidity.plus.com
Fri Jun 16 19:35:42 EDT 2017
[cross-posted to python-ideas]
On 16/06/17 12:32, Robert Vanden Eynde wrote:
> Hello, I would like to propose an idea for the language but I don't know
> where I can talk about it.
Can you please explain what the problem is that you are trying to solve?
> In a nutshell, I would like to be able to write:
> y = (b+2 for b = a + 1)
The above is (almost) equivalent to:
y = (a+1)+2
I realize the parentheses are not required, but I've included them
because if your example mixed operators with different precedence then
they might be necessary.
Other than binding 'b' (you haven't defined what you expect the scope of
that to be, but I'll assume it's the outer scope for now), what is it
about the form you're proposing that's different?
> Or in list comprehension:
> Y = [b+2 for a in L for b = a+1]
> Which can already be done like this:
> Y = [b+2 for a in L for b in [a+1]]
Y = [(a+1)+2 for a in L]
> Which is less obvious, has a small overhead (iterating over a list) and
> get messy with multiple assignment:
> Y = [b+c+2 for a in L for b,c in [(a+1,a+2)]]
> New syntax would allow to write:
> Y = [b+c+2 for a in L for b,c = (a+1,a+2)]
Y = [(a+1)+(a+2)+2 for a in L]
> My first example (b+2 for b = a+1) can already be done using ugly syntax
> using lambda
> y = (lambda b: b+2)(b=a+1)
> y = (lambda b: b+2)(a+1)
> y = (lambda b=a+1: b+2)()
> Choice of syntax: for is good because it uses current keyword, and the
> analogy for x = 5 vs for x in  is natural.
> But the "for" loses the meaning of iteration.
> The use of "with" would maybe sound more logical.
> Python already have the "functional if", lambdas, list comprehension,
> but not simple assignment functional style.
Can you present an example that can't be re-written simply by reducing
the expression as I have done above?
More information about the Python-ideas