[Python-ideas] PEP 572: Statement-Local Name Bindings

David Mertz mertz at gnosis.cx
Wed Feb 28 20:21:55 EST 2018


On Tue, Feb 27, 2018 at 11:46 PM, Matt Arcidy <marcidy at gmail.com> wrote:

> From readability, the examples put forth have been to explain the
> advantage, with which I agree.  However, i do not believe this scales well.
>
> [(foo(x,y) as g)*(bar(y) as i) + g*foo(x,a) +baz(g,i) for x... for y...]
>

This definitely looks hard to read.  Let's compare it to:

lst = []
for x in something:
    for y in other_thing:
        g = f(x, y)
        i = bar(y)
        lst.append(g*foo(x,a) + baz(g,i))

Obviously the one-liner is shorter, but the full loop looks a heck of a lot
more readable to me.

I was thinking of an example closer to the PEP like this:

[((my_object.calculate_the_quantity(quant1, vect2, arr3) as x), log(x)) for
quant1 in quants]

Just one "as" clause, but a long enough expression I wouldn't want to
repeat it.  I still feel this suffers in readability compared to the
existing option of (even as a non-unrolled comprehension):

[(x, log(x)) for x in (my_object.calculate_the_quantity(quant1, vect2,
arr3) for quant1 in quants)]


Sure, again we save a couple characters under the PEP, but readability
feels harmed not helped.  And most likely this is another thing better
spelled as a regular loop.

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180228/434b9a7e/attachment.html>


More information about the Python-ideas mailing list