[Python-ideas] PEP 572: Assignment Expressions (post #4)

George Leslie-Waksman waksman at gmail.com
Wed Apr 11 17:34:13 EDT 2018


I really like this proposal in the context of `while` loops but I'm
lukewarm in other contexts.

I specifically like what this would do for repeated calls.

Being able to replace all of these

md5 = hashlib.md5()
with open(filename, 'rb') as file_reader:
    for chunk in iter(lambda: file_reader.read(1024), b''):
        md5.update(chunk)

md5 = hashlib.md5()
with open(filename, 'rb') as file_reader:
    while True:
        chunk = file_reader.read(1024)
        if not chunk:
            break
        md5.update(chunk)

md5 = hashlib.md5()
with open(filename, 'rb') as file_reader:
    chunk = file_reader.read(1024)
    while chunk:
        md5.update(chunk)
        chunk = file_reader.read(1024)

with

md5 = hashlib.md5()
with open(filename, 'rb') as file_reader:
    while chunk := file_reader.read(1024):
        md5.update(chunk)

seems really nice. I'm not sure the other complexity is justified by this
nicety and I'm really wary of anything that makes comprehensions more
complicated; I already see enough comprehension abuse to the point of
illegibility.

--George



On Wed, Apr 11, 2018 at 10:51 AM Brendan Barnwell <brenbarn at brenbarn.net>
wrote:

> On 2018-04-11 05:23, Clint Hepner wrote:
> > I find the assignments make it difficult to pick out what the final
> expression looks like.
>
>         I strongly agree with this, and for me I think this is enough to
> push
> me to -1 on the whole proposal.  For me the classic example case is
> still the quadratic formula type of thing:
>
> x1, x2 = (-b + sqrt(b**2 - 4*a*c))/2, (-b - sqrt(b**2 - 4*a*c))/2
>
>         It just doesn't seem worth it to me to create an expression-level
> assignment unless it can make things like this not just less verbose but
> at the same time more readable.  I don't consider this more readable:
>
> x1, x2 = (-b + sqrt(D := b**2 - 4*a*c)))/2, (-b - sqrt(D))/2
>
> . . . because having to put the assignment inline creates a visual
> asymmetry, when for me the entire goal of an expression-level statement
> is to make the symmetry between such things MORE obvious.  I want to be
> able to write:
>
> x1, x2 = (-b + sqrt(D)))/2, (-b - sqrt(D))/2 ...
>
> . . . where "..." stands for "the part of the expression where I define
> the variables I'm re-using in multiple places in the expression".
>
>         The new proposal does at least have the advantage that it would
> help
> with things like this:
>
> while x := some_function_call():
>         # do stuff
>
>         So maybe I'm -0.5 rather than -1.  But it's not just that this
> proposal
> "could be used to create ugly code".  It's that using it for
> expression-internal assignments WILL create ugly code, and there's no
> way to avoid it.  I just don't see how this proposal provides any way to
> make things like the quadratic formula example above MORE readable.
>
> --
> Brendan Barnwell
> "Do not follow where the path may lead.  Go, instead, where there is no
> path, and leave a trail."
>     --author unknown
> _______________________________________________
> 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/20180411/e4a939c8/attachment-0001.html>


More information about the Python-ideas mailing list