Better while loop

Well, I’m not sure it provides enough meaningful advantage for a beginner over the more idiomatic, widely used and PEP 8-conforming but otherwise identical alternative using parenthesis continuation to justify recommending it. And like you say, such fine details of style are getting to the point of near orthogonality to a useful answer to their question, I worry they may only confuse a beginner given backlash line continuation is rarely ever used in new code.

I’m not sure I understand what you mean, sorry, as I’m not familiar with the term “linear connector”. The parenthesis continuation example I presented lines everything up just the same as using legacy backslash continuation, just like yours (at the cost of lining up the operators).

Where line continuation is done in other languages and formats with an explicit character, it is generally done with an escape character, which is nearly always a backslash. Furthermore, in Python, it follows from the standard function of the backslash in strings (and in general) as an escape character—it escapes the character which follows in, which when placed the the end of the line is the EOL character. While this is not necessarily intuitive to many/most newer users (and even some more experienced ones), adding a brand new arbitrary character just to perform this function would very likely only add to the potential confusion, especially since it seems a large majority of Python developers tend to prefer parenthesis continuation instead for their new code.

Yes, but does any modern, widely used GUI editor/IDE not offer automatic parenthesis matching highlighting, usually enabled by default? Code folding, line highlighting and indent guides also often also assist with this, such that I don’t personally find this an actual problem in practice.

Also, FYI, you can embed actual Markdown footnotes by doing this: ^[a footnote] which renders as [1].

Yes, but these days a few extra lines are a pretty small price to pay for greater clarity, readability, style consistency and cleaner diffs (on that last point, by contrast, using backlash line continuation you’d have to constantly add additional line continuations to previous lines when adding new lines or moving around existing, much like the much-hated lack of trailing commas in JSON).

At the time that PEP was written, Python used a LL(1) parser that did not allow parenthesis line continuation to be used in certain contexts, such as with statements and others, as referred to by the PEP’s rejection notice. Since then, following further improvements and then the switch to the new PEG parser, those restrictions are now removed. Also, while anecdotal, backslash line continuation was a lot more common in contemporary code back then versus today, and Python code style is much more unified around PEP 8 (particularly Black’s take on it). As such, its plausible that the SC would make a different decision today, though it doesn’t seems like a major priority for the Python 3.x series.


  1. a footnote ↩︎

2 Likes

Thank you. Yes, I’m aware of that. I use the [n] notatation because the footnote coloration is very dark and disappears in the Dark theme with the small font of the superscript. The [n] notation doesn’t give you the convenience of a hyperlink, but at least it’s visible.

1 Like