2007-04-30 19:03:34 -04:00
|
|
|
|
PEP: 3125
|
|
|
|
|
Title: Remove Backslash Continuation
|
|
|
|
|
Version: $Revision$
|
|
|
|
|
Last-Modified: $Date$
|
|
|
|
|
Author: Jim J. Jewett <JimJJewett@gmail.com>
|
2007-05-10 18:18:18 -04:00
|
|
|
|
Status: Rejected
|
2007-04-30 19:03:34 -04:00
|
|
|
|
Type: Standards Track
|
2007-05-04 15:26:28 -04:00
|
|
|
|
Content-Type: text/x-rst
|
2007-04-30 19:03:34 -04:00
|
|
|
|
Created: 29-Apr-2007
|
2007-05-04 15:26:28 -04:00
|
|
|
|
Post-History: 29-Apr-2007, 30-Apr-2007, 04-May-2007
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
2007-05-10 18:18:18 -04:00
|
|
|
|
Rejection Notice
|
|
|
|
|
================
|
|
|
|
|
|
|
|
|
|
This PEP is rejected. There wasn't enough support in favor, the
|
|
|
|
|
feature to be removed isn't all that harmful, and there are some use
|
|
|
|
|
cases that would become harder.
|
|
|
|
|
|
|
|
|
|
|
2007-04-30 19:03:34 -04:00
|
|
|
|
Abstract
|
2007-05-04 15:26:28 -04:00
|
|
|
|
========
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Python initially inherited its parsing from C. While this has been
|
|
|
|
|
generally useful, there are some remnants which have been less useful
|
|
|
|
|
for Python, and should be eliminated.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
This PEP proposes elimination of terminal ``\`` as a marker for line
|
|
|
|
|
continuation.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Motivation
|
|
|
|
|
==========
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
One goal for Python 3000 should be to simplify the language by
|
|
|
|
|
removing unnecessary or duplicated features. There are currently
|
|
|
|
|
several ways to indicate that a logical line is continued on the
|
|
|
|
|
following physical line.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
The other continuation methods are easily explained as a logical
|
|
|
|
|
consequence of the semantics they provide; ``\`` is simply an escape
|
|
|
|
|
character that needs to be memorized.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Existing Line Continuation Methods
|
|
|
|
|
==================================
|
|
|
|
|
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Parenthetical Expression - ``([{}])``
|
|
|
|
|
-------------------------------------
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Open a parenthetical expression. It doesn't matter whether people
|
|
|
|
|
view the "line" as continuing; they do immediately recognize that the
|
|
|
|
|
expression needs to be closed before the statement can end.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Examples using each of ``()``, ``[]``, and ``{}``::
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
def fn(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"]
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Note that it is always possible to parenthesize an expression, but it
|
|
|
|
|
can seem odd to parenthesize an expression that needs parentheses only
|
|
|
|
|
for the line break::
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
assert val>4, (
|
|
|
|
|
"val is too small")
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Triple-Quoted Strings
|
|
|
|
|
---------------------
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Open a triple-quoted string; again, people recognize that the string
|
|
|
|
|
needs to finish before the next statement starts. ::
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
banner_message = """
|
|
|
|
|
Satisfaction Guaranteed,
|
|
|
|
|
or DOUBLE YOUR MONEY BACK!!!
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
some minor restrictions apply"""
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
2007-05-04 15:26:28 -04:00
|
|
|
|
Terminal ``\`` in the general case
|
|
|
|
|
----------------------------------
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
A terminal ``\`` indicates that the logical line is continued on the
|
|
|
|
|
following physical line (after whitespace). There are no particular
|
|
|
|
|
semantics associated with this. This form is never required, although
|
|
|
|
|
it may look better (particularly for people with a C language
|
|
|
|
|
background) in some cases::
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
>>> assert val>4, \
|
|
|
|
|
"val is too small"
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Also note that the ``\`` must be the final character in the line. If
|
|
|
|
|
your editor navigation can add whitespace to the end of a line, that
|
|
|
|
|
invisible change will alter the semantics of the program.
|
|
|
|
|
Fortunately, the typical result is only a syntax error, rather than a
|
|
|
|
|
runtime bug::
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
>>> assert val>4, \
|
|
|
|
|
"val is too small"
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
SyntaxError: unexpected character after line continuation character
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
This PEP proposes to eliminate this redundant and potentially
|
|
|
|
|
confusing alternative.
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
2007-05-04 15:26:28 -04:00
|
|
|
|
Terminal ``\`` within a string
|
|
|
|
|
------------------------------
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
A terminal ``\`` within a single-quoted string, at the end of the
|
|
|
|
|
line. This is arguably a special case of the terminal ``\``, but it
|
|
|
|
|
is a special case that may be worth keeping. ::
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
>>> "abd\
|
|
|
|
|
def"
|
|
|
|
|
'abd def'
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
* Pro: Many of the objections to removing ``\`` termination were
|
|
|
|
|
really just objections to removing it within literal strings;
|
|
|
|
|
several people clarified that they want to keep this literal-string
|
|
|
|
|
usage, but don't mind losing the general case.
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
* Pro: The use of ``\`` for an escape character within strings is well
|
|
|
|
|
known.
|
2007-05-04 16:08:39 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
* Contra: But note that this particular usage is odd, because the
|
|
|
|
|
escaped character (the newline) is invisible, and the special
|
|
|
|
|
treatment is to delete the character. That said, the ``\`` of
|
|
|
|
|
``\(newline)`` is still an escape which changes the meaning of the
|
|
|
|
|
following character.
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
2007-05-04 15:26:28 -04:00
|
|
|
|
Alternate Proposals
|
|
|
|
|
===================
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Several people have suggested alternative ways of marking the line
|
|
|
|
|
end. Most of these were rejected for not actually simplifying things.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
The one exception was to let any unfinished expression signify a line
|
|
|
|
|
continuation, possibly in conjunction with increased indentation.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
This is attractive because it is a generalization of the rule for
|
|
|
|
|
parentheses.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
The initial objections to this were:
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
- The amount of whitespace may be contentious; expression continuation
|
|
|
|
|
should not be confused with opening a new suite.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
- The "expression continuation" markers are not as clearly marked in
|
|
|
|
|
Python as the grouping punctuation "(), [], {}" marks are::
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
# Plus needs another operand, so the line continues
|
|
|
|
|
"abc" +
|
|
|
|
|
"def"
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
# 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"
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
- Guido objected for technical reasons. [#dedent]_ The most obvious
|
|
|
|
|
implementation would require allowing INDENT or DEDENT tokens
|
|
|
|
|
anywhere, or at least in a widely expanded (and ill-defined) set of
|
|
|
|
|
locations. While this is of concern only for the internal parsing
|
|
|
|
|
mechanism (rather than for users), it would be a major new source of
|
|
|
|
|
complexity.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Andrew Koenig then pointed out [#lexical]_ a better implementation
|
|
|
|
|
strategy, and said that it had worked quite well in other
|
|
|
|
|
languages. [#snocone]_ The improved suggestion boiled down to:
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
The whitespace that follows an (operator or) open bracket or
|
|
|
|
|
parenthesis can include newline characters.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
It would be implemented at a very low lexical level -- even before
|
|
|
|
|
the decision is made to turn a newline followed by spaces into an
|
|
|
|
|
INDENT or DEDENT token.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
There is still some concern that it could mask bugs, as in this
|
|
|
|
|
example [#guidobughide]_::
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
# Used to be y+1, the 1 got dropped. Syntax Error (today)
|
|
|
|
|
# would become nonsense.
|
|
|
|
|
x = y+
|
|
|
|
|
f(x)
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
Requiring that the continuation be indented more than the initial line
|
|
|
|
|
would add both safety and complexity.
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Open Issues
|
|
|
|
|
===========
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
* Should ``\``-continuation be removed even inside strings?
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
* Should the continuation markers be expanded from just ([{}]) to
|
|
|
|
|
include lines ending with an operator?
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
* As a safety measure, should the continuation line be required to be
|
|
|
|
|
more indented than the initial line?
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
References
|
2007-05-04 15:26:28 -04:00
|
|
|
|
==========
|
2007-05-04 16:08:39 -04:00
|
|
|
|
|
2007-05-04 15:26:28 -04:00
|
|
|
|
.. [#dedent] (email subject) PEP 30XZ: Simplified Parsing, van Rossum
|
2017-06-11 15:02:39 -04:00
|
|
|
|
https://mail.python.org/pipermail/python-3000/2007-April/007063.html
|
2007-05-04 15:26:28 -04:00
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
.. [#lexical] (email subject) :pep:`3125` -- remove backslash
|
2007-05-04 15:26:28 -04:00
|
|
|
|
continuation, Koenig
|
2017-06-11 15:02:39 -04:00
|
|
|
|
https://mail.python.org/pipermail/python-3000/2007-May/007237.html
|
2007-05-04 16:08:39 -04:00
|
|
|
|
|
2007-05-04 15:26:28 -04:00
|
|
|
|
.. [#snocone] The Snocone Programming Language, Koenig
|
|
|
|
|
http://www.snobol4.com/report.htm
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
|
.. [#guidobughide] (email subject) :pep:`3125` -- remove backslash
|
2007-05-04 15:26:28 -04:00
|
|
|
|
continuation, van Rossum
|
2017-06-11 15:02:39 -04:00
|
|
|
|
https://mail.python.org/pipermail/python-3000/2007-May/007244.html
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
2007-05-04 15:26:28 -04:00
|
|
|
|
=========
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
This document has been placed in the public domain.
|
2007-04-30 19:03:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-04 16:46:34 -04:00
|
|
|
|
..
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
sentence-end-double-space: t
|
|
|
|
|
fill-column: 70
|
|
|
|
|
coding: utf-8
|
|
|
|
|
End:
|