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

Kirill Balunov kirillbalunov at gmail.com
Sat May 12 06:07:50 EDT 2018


2018-05-12 11:36 GMT+03:00 Angus Hollands <goosey15 at gmail.com>:

> Concerning my previous email,
>
> Yes, my mistake. I'd forgotten (ironically) that the whole point is that
> it's an expression itself.
>
> So
> > while (value:=get_next_pool_item()).in_use:
> >    print(value.refcount())
> would be the appropriate analogue.
>
> Consequently, my example is invalid. A better example would have been
> where one needs to access more than one attribute of the expression
>
> while (node.x, node.y) > (5.0, 5.0) given node = get_neighbours(node):
>     pass
>


In addition, this form gives you all the advantages of tuple unpacking:

while (x, y) > (5.0, 5.0) given x, y = get_neighbours(node):
    pass

There was some criticism about the length of the `given`, maybe it is
possible to _repurpose_ `with` keyword:

while (x, y) > (5.0, 5.0) with x, y = get_neighbours(node):
    pass


In this context, the with statement and with expression are clearly
distinguishable both for the parser and for the person. But maybe many will
find this as a bad style ... since the semantics are too different.

With kind regards,
-gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180512/e70d2cbb/attachment.html>


More information about the Python-ideas mailing list