[Python-ideas] Inline assignments using "given" clauses

Peter O'Connor peter.ed.oconnor at gmail.com
Sun May 13 05:23:07 EDT 2018


 *Correction: Above code should read:

    outputs = []
    state = initial_state
    for inp in inputs:
        out, state = my_update_func(inp, state)
        outputs.append(out)


On Sun, May 13, 2018 at 11:21 AM, Peter O'Connor <peter.ed.oconnor at gmail.com
> wrote:

>   target := expr
>   expr as target
>   expr -> target
>   target given target = expr
>   let target = expr
>  : target expr ;
>
>
> Although in general "target:=exp" seems the most palatable of these to me,
> there is one nice benefit to the "given" syntax:
>
> Suppose you have a comprehension wherein you want to pass forward an
> internal "state" between iterations, but not return it as the output:
>
> In today's python, you'd to:
>
>     outputs = []
>     state = initial_state
>     for inp in inputs:
>         out, state = my_update_func(state)
>         outputs.append(state)
>
> This could not be neatly compacted into:
>
>     state = initial_state
>     outputs = [out given out, state = my_update_func(inp, state) for inp
> in inputs]
>
> Or maybe:
>
>     outputs = [out given out, state = my_update_func(inp, state) for inp
> in inputs given state=initial_state]
>
> Though I agree for the much more common case of assigning a value inline
> "x given x=y" seems messily redundant.
>
>
>
> On Sat, May 12, 2018 at 10:37 PM, Stephen J. Turnbull <
> turnbull.stephen.fw at u.tsukuba.ac.jp> wrote:
>
>> David Mertz writes:
>>
>>  > Only the BDFL has a vote with non-zero weight.
>>
>> "Infinitesimal" != "zero".
>>
>> Pedantically yours,
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180513/3365cc4e/attachment-0001.html>


More information about the Python-ideas mailing list