[Python-Dev] Language proposal: variable assignment in functional context

Robert Vanden Eynde robertvandeneynde at hotmail.com
Fri Jun 16 07:32:19 EDT 2017

Hello, I would like to propose an idea for the language but I don't know where I can talk about it.

In a nutshell, I would like to be able to write:
y = (b+2 for b = a + 1)

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]]

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)]

The parenthesis are needed for syntax reason I think.

One could have successive assignements

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 [5] 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170616/54bb4533/attachment.html>

More information about the Python-Dev mailing list