[Python-ideas] Inline assignment expression

Jervis Whitley jervisau at gmail.com
Sat Mar 14 14:47:01 CET 2009

http://bugs.python.org/issue1714448 had an interesting proposal that I thought
might be worthwhile discussing here.

if something as x:

however, of greater use would be assignment expressions that allow:
if (something as x) == other:
    # can now use x.

I propose that we implement assignment expressions that would allow assignments
to be made any place that expressions are currently valid. The
proposal uses the
(nominal) right arrow (RARROW) '->' to indicate the assignment. The
form would look
like this:

    EXPR -> VAR

which translates to

    VAR = EXPR

Expression (EXPR) is evaluated and assigned to target VAR. The value of EXPR is
left on the top of stack.

another toy example to think about:

    while len(expensive() -> res) == 4:

A patch has been uploaded to the named issue in the bug tracker. I encourage
you to try it out (py3k at the moment). As I mentioned earlier the
exact syntax is only nominal. We needn't use the RARROW if consensus
is against that, it is a simple operation to change this to any of
('becomes', 'into', 'assigns' ...

I look forward to your comments.



More information about the Python-ideas mailing list