CINXE.COM

PEP 3125 – Remove Backslash Continuation | peps.python.org

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="color-scheme" content="light dark"> <title>PEP 3125 – Remove Backslash Continuation | peps.python.org</title> <link rel="shortcut icon" href="../_static/py.png"> <link rel="canonical" href="https://peps.python.org/pep-3125/"> <link rel="stylesheet" href="../_static/style.css" type="text/css"> <link rel="stylesheet" href="../_static/mq.css" type="text/css"> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" media="(prefers-color-scheme: light)" id="pyg-light"> <link rel="stylesheet" href="../_static/pygments_dark.css" type="text/css" media="(prefers-color-scheme: dark)" id="pyg-dark"> <link rel="alternate" type="application/rss+xml" title="Latest PEPs" href="https://peps.python.org/peps.rss"> <meta property="og:title" content='PEP 3125 – Remove Backslash Continuation | peps.python.org'> <meta property="og:description" content="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."> <meta property="og:type" content="website"> <meta property="og:url" content="https://peps.python.org/pep-3125/"> <meta property="og:site_name" content="Python Enhancement Proposals (PEPs)"> <meta property="og:image" content="https://peps.python.org/_static/og-image.png"> <meta property="og:image:alt" content="Python PEPs"> <meta property="og:image:width" content="200"> <meta property="og:image:height" content="200"> <meta name="description" content="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."> <meta name="theme-color" content="#3776ab"> </head> <body> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="svg-sun-half" viewBox="0 0 24 24" pointer-events="all"> <title>Following system colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="9"></circle> <path d="M12 3v18m0-12l4.65-4.65M12 14.3l7.37-7.37M12 19.6l8.85-8.85"></path> </svg> </symbol> <symbol id="svg-moon" viewBox="0 0 24 24" pointer-events="all"> <title>Selected dark colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"></path> <path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z"></path> </svg> </symbol> <symbol id="svg-sun" viewBox="0 0 24 24" pointer-events="all"> <title>Selected light colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="5"></circle> <line x1="12" y1="1" x2="12" y2="3"></line> <line x1="12" y1="21" x2="12" y2="23"></line> <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line> <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line> <line x1="1" y1="12" x2="3" y2="12"></line> <line x1="21" y1="12" x2="23" y2="12"></line> <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line> <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line> </svg> </symbol> </svg> <script> document.documentElement.dataset.colour_scheme = localStorage.getItem("colour_scheme") || "auto" </script> <section id="pep-page-section"> <header> <h1>Python Enhancement Proposals</h1> <ul class="breadcrumbs"> <li><a href="https://www.python.org/" title="The Python Programming Language">Python</a> &raquo; </li> <li><a href="../pep-0000/">PEP Index</a> &raquo; </li> <li>PEP 3125</li> </ul> <button id="colour-scheme-cycler" onClick="setColourScheme(nextColourScheme())"> <svg aria-hidden="true" class="colour-scheme-icon-when-auto"><use href="#svg-sun-half"></use></svg> <svg aria-hidden="true" class="colour-scheme-icon-when-dark"><use href="#svg-moon"></use></svg> <svg aria-hidden="true" class="colour-scheme-icon-when-light"><use href="#svg-sun"></use></svg> <span class="visually-hidden">Toggle light / dark / auto colour theme</span> </button> </header> <article> <section id="pep-content"> <h1 class="page-title">PEP 3125 – Remove Backslash Continuation</h1> <dl class="rfc2822 field-list simple"> <dt class="field-odd">Author<span class="colon">:</span></dt> <dd class="field-odd">Jim J. Jewett &lt;JimJJewett&#32;&#97;t&#32;gmail.com&gt;</dd> <dt class="field-even">Status<span class="colon">:</span></dt> <dd class="field-even"><abbr title="Formally declined and will not be accepted">Rejected</abbr></dd> <dt class="field-odd">Type<span class="colon">:</span></dt> <dd class="field-odd"><abbr title="Normative PEP with a new feature for Python, implementation change for CPython or interoperability standard for the ecosystem">Standards Track</abbr></dd> <dt class="field-even">Created<span class="colon">:</span></dt> <dd class="field-even">29-Apr-2007</dd> <dt class="field-odd">Post-History<span class="colon">:</span></dt> <dd class="field-odd">29-Apr-2007, 30-Apr-2007, 04-May-2007</dd> </dl> <hr class="docutils" /> <section id="contents"> <details><summary>Table of Contents</summary><ul class="simple"> <li><a class="reference internal" href="#rejection-notice">Rejection Notice</a></li> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#motivation">Motivation</a></li> <li><a class="reference internal" href="#existing-line-continuation-methods">Existing Line Continuation Methods</a><ul> <li><a class="reference internal" href="#parenthetical-expression">Parenthetical Expression - <code class="docutils literal notranslate"><span class="pre">([{}])</span></code></a></li> <li><a class="reference internal" href="#triple-quoted-strings">Triple-Quoted Strings</a></li> <li><a class="reference internal" href="#terminal-in-the-general-case">Terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> in the general case</a></li> <li><a class="reference internal" href="#terminal-within-a-string">Terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> within a string</a></li> </ul> </li> <li><a class="reference internal" href="#alternate-proposals">Alternate Proposals</a></li> <li><a class="reference internal" href="#open-issues">Open Issues</a></li> <li><a class="reference internal" href="#references">References</a></li> <li><a class="reference internal" href="#copyright">Copyright</a></li> </ul> </details></section> <section id="rejection-notice"> <h2><a class="toc-backref" href="#rejection-notice" role="doc-backlink">Rejection Notice</a></h2> <p>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.</p> </section> <section id="abstract"> <h2><a class="toc-backref" href="#abstract" role="doc-backlink">Abstract</a></h2> <p>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.</p> <p>This PEP proposes elimination of terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> as a marker for line continuation.</p> </section> <section id="motivation"> <h2><a class="toc-backref" href="#motivation" role="doc-backlink">Motivation</a></h2> <p>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.</p> <p>The other continuation methods are easily explained as a logical consequence of the semantics they provide; <code class="docutils literal notranslate"><span class="pre">\</span></code> is simply an escape character that needs to be memorized.</p> </section> <section id="existing-line-continuation-methods"> <h2><a class="toc-backref" href="#existing-line-continuation-methods" role="doc-backlink">Existing Line Continuation Methods</a></h2> <section id="parenthetical-expression"> <h3><a class="toc-backref" href="#parenthetical-expression" role="doc-backlink">Parenthetical Expression - <code class="docutils literal notranslate"><span class="pre">([{}])</span></code></a></h3> <p>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.</p> <p>Examples using each of <code class="docutils literal notranslate"><span class="pre">()</span></code>, <code class="docutils literal notranslate"><span class="pre">[]</span></code>, and <code class="docutils literal notranslate"><span class="pre">{}</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">fn</span><span class="p">(</span><span class="n">long_argname1</span><span class="p">,</span> <span class="n">long_argname2</span><span class="p">):</span> <span class="n">settings</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;background&quot;</span><span class="p">:</span> <span class="s2">&quot;random noise&quot;</span><span class="p">,</span> <span class="s2">&quot;volume&quot;</span><span class="p">:</span> <span class="s2">&quot;barely audible&quot;</span><span class="p">}</span> <span class="n">restrictions</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Warrantee void if used&quot;</span><span class="p">,</span> <span class="s2">&quot;Notice must be received by yesterday&quot;</span><span class="p">,</span> <span class="s2">&quot;Not responsible for sales pitch&quot;</span><span class="p">]</span> </pre></div> </div> <p>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:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">assert</span> <span class="n">val</span><span class="o">&gt;</span><span class="mi">4</span><span class="p">,</span> <span class="p">(</span> <span class="s2">&quot;val is too small&quot;</span><span class="p">)</span> </pre></div> </div> </section> <section id="triple-quoted-strings"> <h3><a class="toc-backref" href="#triple-quoted-strings" role="doc-backlink">Triple-Quoted Strings</a></h3> <p>Open a triple-quoted string; again, people recognize that the string needs to finish before the next statement starts.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">banner_message</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span> <span class="s2"> Satisfaction Guaranteed,</span> <span class="s2"> or DOUBLE YOUR MONEY BACK!!!</span> <span class="s2"> some minor restrictions apply&quot;&quot;&quot;</span> </pre></div> </div> </section> <section id="terminal-in-the-general-case"> <h3><a class="toc-backref" href="#terminal-in-the-general-case" role="doc-backlink">Terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> in the general case</a></h3> <p>A terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> 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:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">val</span><span class="o">&gt;</span><span class="mi">4</span><span class="p">,</span> \ <span class="go"> &quot;val is too small&quot;</span> </pre></div> </div> <p>Also note that the <code class="docutils literal notranslate"><span class="pre">\</span></code> 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:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">val</span><span class="o">&gt;</span><span class="mi">4</span><span class="p">,</span> \ <span class="go"> &quot;val is too small&quot;</span> <span class="go">SyntaxError: unexpected character after line continuation character</span> </pre></div> </div> <p>This PEP proposes to eliminate this redundant and potentially confusing alternative.</p> </section> <section id="terminal-within-a-string"> <h3><a class="toc-backref" href="#terminal-within-a-string" role="doc-backlink">Terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> within a string</a></h3> <p>A terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> within a single-quoted string, at the end of the line. This is arguably a special case of the terminal <code class="docutils literal notranslate"><span class="pre">\</span></code>, but it is a special case that may be worth keeping.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s2">&quot;abd</span><span class="se">\</span> <span class="go"> def&quot;</span> <span class="go">&#39;abd def&#39;</span> </pre></div> </div> <ul class="simple"> <li>Pro: Many of the objections to removing <code class="docutils literal notranslate"><span class="pre">\</span></code> 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.</li> <li>Pro: The use of <code class="docutils literal notranslate"><span class="pre">\</span></code> for an escape character within strings is well known.</li> <li>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 <code class="docutils literal notranslate"><span class="pre">\</span></code> of <code class="docutils literal notranslate"><span class="pre">\(newline)</span></code> is still an escape which changes the meaning of the following character.</li> </ul> </section> </section> <section id="alternate-proposals"> <h2><a class="toc-backref" href="#alternate-proposals" role="doc-backlink">Alternate Proposals</a></h2> <p>Several people have suggested alternative ways of marking the line end. Most of these were rejected for not actually simplifying things.</p> <p>The one exception was to let any unfinished expression signify a line continuation, possibly in conjunction with increased indentation.</p> <p>This is attractive because it is a generalization of the rule for parentheses.</p> <p>The initial objections to this were:</p> <ul> <li>The amount of whitespace may be contentious; expression continuation should not be confused with opening a new suite.</li> <li>The “expression continuation” markers are not as clearly marked in Python as the grouping punctuation “(), [], {}” marks are:<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Plus needs another operand, so the line continues</span> <span class="s2">&quot;abc&quot;</span> <span class="o">+</span> <span class="s2">&quot;def&quot;</span> <span class="c1"># String ends an expression, so the line does not</span> <span class="c1"># not continue. The next line is a syntax error because</span> <span class="c1"># unary plus does not apply to strings.</span> <span class="s2">&quot;abc&quot;</span> <span class="o">+</span> <span class="s2">&quot;def&quot;</span> </pre></div> </div> </li> <li>Guido objected for technical reasons. <a class="footnote-reference brackets" href="#dedent" id="id1">[1]</a> 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.</li> </ul> <p>Andrew Koenig then pointed out <a class="footnote-reference brackets" href="#lexical" id="id2">[2]</a> a better implementation strategy, and said that it had worked quite well in other languages. <a class="footnote-reference brackets" href="#snocone" id="id3">[3]</a> The improved suggestion boiled down to:</p> <blockquote> <div>The whitespace that follows an (operator or) open bracket or parenthesis can include newline characters.<p>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.</p> </div></blockquote> <p>There is still some concern that it could mask bugs, as in this example <a class="footnote-reference brackets" href="#guidobughide" id="id4">[4]</a>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Used to be y+1, the 1 got dropped. Syntax Error (today)</span> <span class="c1"># would become nonsense.</span> <span class="n">x</span> <span class="o">=</span> <span class="n">y</span><span class="o">+</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> </pre></div> </div> <p>Requiring that the continuation be indented more than the initial line would add both safety and complexity.</p> </section> <section id="open-issues"> <h2><a class="toc-backref" href="#open-issues" role="doc-backlink">Open Issues</a></h2> <ul class="simple"> <li>Should <code class="docutils literal notranslate"><span class="pre">\</span></code>-continuation be removed even inside strings?</li> <li>Should the continuation markers be expanded from just ([{}]) to include lines ending with an operator?</li> <li>As a safety measure, should the continuation line be required to be more indented than the initial line?</li> </ul> </section> <section id="references"> <h2><a class="toc-backref" href="#references" role="doc-backlink">References</a></h2> <aside class="footnote-list brackets"> <aside class="footnote brackets" id="dedent" role="doc-footnote"> <dt class="label" id="dedent">[<a href="#id1">1</a>]</dt> <dd>(email subject) PEP 30XZ: Simplified Parsing, van Rossum <a class="reference external" href="https://mail.python.org/pipermail/python-3000/2007-April/007063.html">https://mail.python.org/pipermail/python-3000/2007-April/007063.html</a></aside> <aside class="footnote brackets" id="lexical" role="doc-footnote"> <dt class="label" id="lexical">[<a href="#id2">2</a>]</dt> <dd>(email subject) <a class="pep reference internal" href="../pep-3125/" title="PEP 3125 – Remove Backslash Continuation">PEP 3125</a> – remove backslash continuation, Koenig <a class="reference external" href="https://mail.python.org/pipermail/python-3000/2007-May/007237.html">https://mail.python.org/pipermail/python-3000/2007-May/007237.html</a></aside> <aside class="footnote brackets" id="snocone" role="doc-footnote"> <dt class="label" id="snocone">[<a href="#id3">3</a>]</dt> <dd>The Snocone Programming Language, Koenig <a class="reference external" href="http://www.snobol4.com/report.htm">http://www.snobol4.com/report.htm</a></aside> <aside class="footnote brackets" id="guidobughide" role="doc-footnote"> <dt class="label" id="guidobughide">[<a href="#id4">4</a>]</dt> <dd>(email subject) <a class="pep reference internal" href="../pep-3125/" title="PEP 3125 – Remove Backslash Continuation">PEP 3125</a> – remove backslash continuation, van Rossum <a class="reference external" href="https://mail.python.org/pipermail/python-3000/2007-May/007244.html">https://mail.python.org/pipermail/python-3000/2007-May/007244.html</a></aside> </aside> </section> <section id="copyright"> <h2><a class="toc-backref" href="#copyright" role="doc-backlink">Copyright</a></h2> <p>This document has been placed in the public domain.</p> </section> </section> <hr class="docutils" /> <p>Source: <a class="reference external" href="https://github.com/python/peps/blob/main/peps/pep-3125.rst">https://github.com/python/peps/blob/main/peps/pep-3125.rst</a></p> <p>Last modified: <a class="reference external" href="https://github.com/python/peps/commits/main/peps/pep-3125.rst">2025-02-01 08:59:27 GMT</a></p> </article> <nav id="pep-sidebar"> <h2>Contents</h2> <ul> <li><a class="reference internal" href="#rejection-notice">Rejection Notice</a></li> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#motivation">Motivation</a></li> <li><a class="reference internal" href="#existing-line-continuation-methods">Existing Line Continuation Methods</a><ul> <li><a class="reference internal" href="#parenthetical-expression">Parenthetical Expression - <code class="docutils literal notranslate"><span class="pre">([{}])</span></code></a></li> <li><a class="reference internal" href="#triple-quoted-strings">Triple-Quoted Strings</a></li> <li><a class="reference internal" href="#terminal-in-the-general-case">Terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> in the general case</a></li> <li><a class="reference internal" href="#terminal-within-a-string">Terminal <code class="docutils literal notranslate"><span class="pre">\</span></code> within a string</a></li> </ul> </li> <li><a class="reference internal" href="#alternate-proposals">Alternate Proposals</a></li> <li><a class="reference internal" href="#open-issues">Open Issues</a></li> <li><a class="reference internal" href="#references">References</a></li> <li><a class="reference internal" href="#copyright">Copyright</a></li> </ul> <br> <a id="source" href="https://github.com/python/peps/blob/main/peps/pep-3125.rst">Page Source (GitHub)</a> </nav> </section> <script src="../_static/colour_scheme.js"></script> <script src="../_static/wrap_tables.js"></script> <script src="../_static/sticky_banner.js"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10