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

Steven D'Aprano steve at pearwood.info
Fri May 11 12:43:14 EDT 2018

On Fri, May 11, 2018 at 03:47:05PM +0200, João Santos wrote:

> How do you read something like " while (cmd := get_command()).token !=
> CMD_QUIT:" in plain english?

I wouldn't if I could avoid it. I hardly ever program by talking about 
code in plain English. Often the lines are gobblydegook:

    zreplace = '%c%02d%02d' % (sign, h, m)  # datetime.py

and even when they are technically pronouncable English:

    # subprocess.py    
    (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) = 
            self._get_handles(stdin, stdout, stderr)

my brain would glaze over by the second "p2c". I prefer to read and 
write code than speak it, and if I need to discuss it, I prefer to use a 
whiteboard so I can write things down.

But if I really needed to, I'd probably start by reading it as:

    while open bracket command defined as get-command close bracket dot 
    token is not equal to command-quit

and then I'd probably drop the "defined" and swap the order around.

Actually, that's not true. I probably wouldn't say that, not in a 
real conversation. What I'd probably say is, 

    So, like, I've got this command object, see, which I get
    from calling get-command, right, and so I get the, um,
    token attribute, okay, and if that's not equal to the 
    quit value, I loop until it is. Right?

(And this is why I prefer *writing* code than *saying* code.)


More information about the Python-ideas mailing list