This PEP is rejected. There wasn’t enough support in favor, thefeature to be removed isn’t all that harmful, and there are some usecases that would become harder.
Python initially inherited its parsing from C. While this has beengenerally useful, there are some remnants which have been less usefulfor Python, and should be eliminated.
This PEP proposes elimination of terminal\ as a marker for linecontinuation.
One goal for Python 3000 should be to simplify the language byremoving unnecessary or duplicated features. There are currentlyseveral ways to indicate that a logical line is continued on thefollowing physical line.
The other continuation methods are easily explained as a logicalconsequence of the semantics they provide;\ is simply an escapecharacter that needs to be memorized.
([{}])Open a parenthetical expression. It doesn’t matter whether peopleview the “line” as continuing; they do immediately recognize that theexpression needs to be closed before the statement can end.
Examples using each of(),[], and{}:
deffn(long_argname1,long_argname2):settings={"background":"random noise","volume":"barely audible"}restrictions=["Warrantee void if used","Notice must be received by yesterday","Not responsible for sales pitch"]
Note that it is always possible to parenthesize an expression, but itcan seem odd to parenthesize an expression that needs parentheses onlyfor the line break:
assertval>4,("val is too small")
Open a triple-quoted string; again, people recognize that the stringneeds to finish before the next statement starts.
banner_message=""" Satisfaction Guaranteed, or DOUBLE YOUR MONEY BACK!!! some minor restrictions apply"""
\ in the general caseA terminal\ indicates that the logical line is continued on thefollowing physical line (after whitespace). There are no particularsemantics associated with this. This form is never required, althoughit may look better (particularly for people with a C languagebackground) in some cases:
>>>assertval>4, \ "val is too small"
Also note that the\ must be the final character in the line. Ifyour editor navigation can add whitespace to the end of a line, thatinvisible change will alter the semantics of the program.Fortunately, the typical result is only a syntax error, rather than aruntime bug:
>>>assertval>4, \ "val is too small"SyntaxError: unexpected character after line continuation character
This PEP proposes to eliminate this redundant and potentiallyconfusing alternative.
\ within a stringA terminal\ within a single-quoted string, at the end of theline. This is arguably a special case of the terminal\, but itis a special case that may be worth keeping.
>>>"abd\ def"'abd def'
\ termination werereally just objections to removing it within literal strings;several people clarified that they want to keep this literal-stringusage, but don’t mind losing the general case.\ for an escape character within strings is wellknown.\ of\(newline) is still an escape which changes the meaning of thefollowing character.Several people have suggested alternative ways of marking the lineend. Most of these were rejected for not actually simplifying things.
The one exception was to let any unfinished expression signify a linecontinuation, possibly in conjunction with increased indentation.
This is attractive because it is a generalization of the rule forparentheses.
The initial objections to this were:
# Plus needs another operand, so the line continues"abc"+"def"# String ends an expression, so the line does not# not continue. The next line is a syntax error because# unary plus does not apply to strings."abc"+"def"
Andrew Koenig then pointed out[2] a better implementationstrategy, and said that it had worked quite well in otherlanguages.[3] The improved suggestion boiled down to:
The whitespace that follows an (operator or) open bracket orparenthesis can include newline characters.It would be implemented at a very low lexical level – even beforethe decision is made to turn a newline followed by spaces into anINDENT or DEDENT token.
There is still some concern that it could mask bugs, as in thisexample[4]:
# Used to be y+1, the 1 got dropped. Syntax Error (today)# would become nonsense.x=y+f(x)
Requiring that the continuation be indented more than the initial linewould add both safety and complexity.
\-continuation be removed even inside strings?This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-3125.rst
Last modified:2025-02-01 08:59:27 GMT