[Python-Dev] PEP 572: Assignment Expressions (original) (raw)

David Mertz mertz at gnosis.cx
Tue Apr 24 08:30:23 EDT 2018


I do think the pronunciation issue that Greg notices is important. I teach Python for most of my living, and reading and discussing code segments is an important part of that. When focussing on how Python actually spells something, you can't always jump to the higher-level meaning of a construct. For some complex expression—whether or not "binding expressions" are added—sometimes it makes sense to give a characterization of the meaning of the expression, but other times you want to say aloud the entire spelling of the expression.

Although feelings are mixed about this, I like the "dunder" contraction for this purpose. It's less of a mouthful to say "dunder-init" than "underscore-underscore-init-underscore-underscore" aloud. And once you learn that shorthand, it's unambiguous.

I think I'd pronounce:

if (diff := x - x_base) and (g := gcd(diff, n)) > 1:
    return g

As:

"If diff bound to x minus x_base (is non-zero), and g bound to gcd of diff comma n is greater than 1, return g"

But having a convention for pronouncing this would be nice, rather than it being my idiosyncrasy.

On Mon, Apr 23, 2018 at 8:23 PM, Tim Peters <tim.peters at gmail.com> wrote:

[Tim] >> if (diff := x - xbase) and (g := gcd(diff, n)) > 1: >> return g

[Greg Ewing <greg.ewing at canterbury.ac.nz>] > My problem with this is -- how do you read such code out loud? In the message in which I first gave that example: if the diff isn't 0 and gcd(diff, n) > 1, return the gcd. That's how I thought of it from the start. In my mind, x - xbase doesn't even exist except as a low-level definition of what "diff" means. It's different for the other test: there g doesn't exist except as a shorthand for "the gcd". In one case it's the name that's important to me, and in the other case the expression. The entire function from which this came is doing all arithmetic modulo n, so n isn't in my mind either - it's a ubiquitous part of the background in this specific function. But you did ask howI would read that code ;-) Anyone else is free to read it however they like. I naturally read it in the way that makes most sense to me in its context.

> From my Pascal days I'm used to reading ":=" as "becomes". So > this says: > > "If diff becomes x - base and g becomes gcd(diff, n) is > greater than or equal to 1 then return g." > > But "diff becomes x - base" is not what we're testing! I don't really follow that. In Python, if f() and g > 1: first tests whether f() "is truthy", regardless of whether it does or doesn't appear in a binding expression. Because this code is working with integers, there's an implied "!= 0" comparison. > That makes it sound like the result of x - base may or may not > get assigned to diff, which is not what's happening at all. Then I suggest the problem you're having doesn't stem from the binding expression, but from that you're omitting to fill in the != 0 part: if you're not thrown by "greater than 1", I can't see how you can be thrown by "not zero". > The "as" variant makes more sense when you read it as an > English sentence: > > if ((x - xbase) as diff) and ... > > "If x - xbase (and by the way, I'm going to call that > diff so I can refer to it later) is not zero ..." So read the original as "if diff (which is x - xbase) is not zero ...". Regardless, Guido has already said "as" is DOA (Dead On Arrival) (illustrating that it's also common enough in English to give a short name before its long-winded meaning ;-) ).


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ mertz%40gnosis.cx

-- 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-dev/attachments/20180424/edd76cf2/attachment-0001.html>



More information about the Python-Dev mailing list