CINXE.COM

PEP 535 – Rich comparison chaining | 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 535 – Rich comparison chaining | peps.python.org</title> <link rel="shortcut icon" href="../_static/py.png"> <link rel="canonical" href="https://peps.python.org/pep-0535/"> <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 535 – Rich comparison chaining | peps.python.org'> <meta property="og:description" content="Inspired by PEP 335, and building on the circuit breaking protocol described in PEP 532, this PEP proposes a change to the definition of chained comparisons, where the comparison chaining will be updated to use the left-associative circuit breaking oper..."> <meta property="og:type" content="website"> <meta property="og:url" content="https://peps.python.org/pep-0535/"> <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="Inspired by PEP 335, and building on the circuit breaking protocol described in PEP 532, this PEP proposes a change to the definition of chained comparisons, where the comparison chaining will be updated to use the left-associative circuit breaking oper..."> <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 535</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 535 – Rich comparison chaining</h1> <dl class="rfc2822 field-list simple"> <dt class="field-odd">Author<span class="colon">:</span></dt> <dd class="field-odd">Alyssa Coghlan &lt;ncoghlan&#32;&#97;t&#32;gmail.com&gt;</dd> <dt class="field-even">Status<span class="colon">:</span></dt> <dd class="field-even"><abbr title="Inactive draft that may be taken up again at a later time">Deferred</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">Requires<span class="colon">:</span></dt> <dd class="field-even"><a class="reference external" href="../pep-0532/">532</a></dd> <dt class="field-odd">Created<span class="colon">:</span></dt> <dd class="field-odd">12-Nov-2016</dd> <dt class="field-even">Python-Version<span class="colon">:</span></dt> <dd class="field-even">3.8</dd> </dl> <hr class="docutils" /> <section id="contents"> <details><summary>Table of Contents</summary><ul class="simple"> <li><a class="reference internal" href="#pep-deferral">PEP Deferral</a></li> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#relationship-with-other-peps">Relationship with other PEPs</a></li> <li><a class="reference internal" href="#specification">Specification</a></li> <li><a class="reference internal" href="#rationale">Rationale</a></li> <li><a class="reference internal" href="#implementation">Implementation</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="pep-deferral"> <h2><a class="toc-backref" href="#pep-deferral" role="doc-backlink">PEP Deferral</a></h2> <p>Further consideration of this PEP has been deferred until Python 3.8 at the earliest.</p> </section> <section id="abstract"> <h2><a class="toc-backref" href="#abstract" role="doc-backlink">Abstract</a></h2> <p>Inspired by <a class="pep reference internal" href="../pep-0335/" title="PEP 335 – Overloadable Boolean Operators">PEP 335</a>, and building on the circuit breaking protocol described in <a class="pep reference internal" href="../pep-0532/" title="PEP 532 – A circuit breaking protocol and binary operators">PEP 532</a>, this PEP proposes a change to the definition of chained comparisons, where the comparison chaining will be updated to use the left-associative circuit breaking operator (<code class="docutils literal notranslate"><span class="pre">else</span></code>) rather than the logical disjunction operator (<code class="docutils literal notranslate"><span class="pre">and</span></code>) if the left hand comparison returns a circuit breaker as its result.</p> <p>While there are some practical complexities arising from the current handling of single-valued arrays in NumPy, this change should be sufficient to allow elementwise chained comparison operations for matrices, where the result is a matrix of boolean values, rather than raising <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> or tautologically returning <code class="docutils literal notranslate"><span class="pre">True</span></code> (indicating a non-empty matrix).</p> </section> <section id="relationship-with-other-peps"> <h2><a class="toc-backref" href="#relationship-with-other-peps" role="doc-backlink">Relationship with other PEPs</a></h2> <p>This PEP has been extracted from earlier iterations of <a class="pep reference internal" href="../pep-0532/" title="PEP 532 – A circuit breaking protocol and binary operators">PEP 532</a>, as a follow-on use case for the circuit breaking protocol, rather than an essential part of its introduction.</p> <p>The specific proposal in this PEP to handle the element-wise comparison use case by changing the semantic definition of comparison chaining is drawn directly from Guido’s rejection of <a class="pep reference internal" href="../pep-0335/" title="PEP 335 – Overloadable Boolean Operators">PEP 335</a>.</p> </section> <section id="specification"> <h2><a class="toc-backref" href="#specification" role="doc-backlink">Specification</a></h2> <p>A chained comparison like <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">10</span></code> written as:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">LEFT_BOUND</span> <span class="n">LEFT_OP</span> <span class="n">EXPR</span> <span class="n">RIGHT_OP</span> <span class="n">RIGHT_BOUND</span> </pre></div> </div> <p>is currently roughly semantically equivalent to:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">_expr</span> <span class="o">=</span> <span class="n">EXPR</span> <span class="n">_lhs_result</span> <span class="o">=</span> <span class="n">LEFT_BOUND</span> <span class="n">LEFT_OP</span> <span class="n">_expr</span> <span class="n">_expr_result</span> <span class="o">=</span> <span class="n">_lhs_result</span> <span class="ow">and</span> <span class="p">(</span><span class="n">_expr</span> <span class="n">RIGHT_OP</span> <span class="n">RIGHT_BOUND</span><span class="p">)</span> </pre></div> </div> <p>Using the circuit breaking concepts introduced in <a class="pep reference internal" href="../pep-0532/" title="PEP 532 – A circuit breaking protocol and binary operators">PEP 532</a>, this PEP proposes that comparison chaining be changed to explicitly check if the left comparison returns a circuit breaker, and if so, use <code class="docutils literal notranslate"><span class="pre">else</span></code> rather than <code class="docutils literal notranslate"><span class="pre">and</span></code> to implement the comparison chaining:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">_expr</span> <span class="o">=</span> <span class="n">EXPR</span> <span class="n">_lhs_result</span> <span class="o">=</span> <span class="n">LEFT_BOUND</span> <span class="n">LEFT_OP</span> <span class="n">_expr</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">_lhs_result</span><span class="p">),</span> <span class="s2">&quot;__else__&quot;</span><span class="p">):</span> <span class="n">_expr_result</span> <span class="o">=</span> <span class="n">_lhs_result</span> <span class="k">else</span> <span class="p">(</span><span class="n">_expr</span> <span class="n">RIGHT_OP</span> <span class="n">RIGHT_BOUND</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">_expr_result</span> <span class="o">=</span> <span class="n">_lhs_result</span> <span class="ow">and</span> <span class="p">(</span><span class="n">_expr</span> <span class="n">RIGHT_OP</span> <span class="n">RIGHT_BOUND</span><span class="p">)</span> </pre></div> </div> <p>This allows types like NumPy arrays to control the behaviour of chained comparisons by returning suitably defined circuit breakers from comparison operations.</p> <p>The expansion of this logic to an arbitrary number of chained comparison operations would be the same as the existing expansion for <code class="docutils literal notranslate"><span class="pre">and</span></code>.</p> </section> <section id="rationale"> <h2><a class="toc-backref" href="#rationale" role="doc-backlink">Rationale</a></h2> <p>In ultimately rejecting <a class="pep reference internal" href="../pep-0335/" title="PEP 335 – Overloadable Boolean Operators">PEP 335</a>, Guido van Rossum noted <a class="footnote-reference brackets" href="#id2" id="id1">[1]</a>:</p> <blockquote> <div>The NumPy folks brought up a somewhat separate issue: for them, the most common use case is chained comparisons (e.g. A &lt; B &lt; C).</div></blockquote> <p>To understand this observation, we first need to look at how comparisons work with NumPy arrays:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">increasing</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">increasing</span> <span class="go">array([0, 1, 2, 3, 4])</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">decreasing</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">decreasing</span> <span class="go">array([4, 3, 2, 1, 0])</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">increasing</span> <span class="o">&lt;</span> <span class="n">decreasing</span> <span class="go">array([ True, True, False, False, False], dtype=bool)</span> </pre></div> </div> <p>Here we see that NumPy array comparisons are element-wise by default, comparing each element in the left hand array to the corresponding element in the right hand array, and producing a matrix of boolean results.</p> <p>If either side of the comparison is a scalar value, then it is broadcast across the array and compared to each individual element:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">0</span> <span class="o">&lt;</span> <span class="n">increasing</span> <span class="go">array([False, True, True, True, True], dtype=bool)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">increasing</span> <span class="o">&lt;</span> <span class="mi">4</span> <span class="go">array([ True, True, True, True, False], dtype=bool)</span> </pre></div> </div> <p>However, this broadcasting idiom breaks down if we attempt to use chained comparisons:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">0</span> <span class="o">&lt;</span> <span class="n">increasing</span> <span class="o">&lt;</span> <span class="mi">4</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span> <span class="gr">ValueError</span>: <span class="n">The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</span> </pre></div> </div> <p>The problem is that internally, Python implicitly expands this chained comparison into the form:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">0</span> <span class="o">&lt;</span> <span class="n">increasing</span> <span class="ow">and</span> <span class="n">increasing</span> <span class="o">&lt;</span> <span class="mi">4</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span> <span class="gr">ValueError</span>: <span class="n">The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</span> </pre></div> </div> <p>And NumPy only permits implicit coercion to a boolean value for single-element arrays where <code class="docutils literal notranslate"><span class="pre">a.any()</span></code> and <code class="docutils literal notranslate"><span class="pre">a.all()</span></code> can be assured of having the same result:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">False</span><span class="p">])</span> <span class="ow">and</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">False</span><span class="p">])</span> <span class="go">array([False], dtype=bool)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">False</span><span class="p">])</span> <span class="ow">and</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">])</span> <span class="go">array([False], dtype=bool)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">])</span> <span class="ow">and</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">False</span><span class="p">])</span> <span class="go">array([False], dtype=bool)</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">])</span> <span class="ow">and</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">])</span> <span class="go">array([ True], dtype=bool)</span> </pre></div> </div> <p>The proposal in this PEP would allow this situation to be changed by updating the definition of element-wise comparison operations in NumPy to return a dedicated subclass that implements the new circuit breaking protocol and also changes the result array’s interpretation in a boolean context to always return <code class="docutils literal notranslate"><span class="pre">False</span></code> and hence never trigger the short-circuiting behaviour:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">ComparisonResultArray</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span> <span class="k">def</span><span class="w"> </span><span class="fm">__bool__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c1"># Element-wise comparison chaining never short-circuits</span> <span class="k">return</span> <span class="kc">False</span> <span class="k">def</span><span class="w"> </span><span class="nf">_raise_NotImplementedError</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;Comparison array truth values are ambiguous outside &quot;</span> <span class="s2">&quot;chained comparisons. Use a.any() or a.all()&quot;</span><span class="p">)</span> <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span> <span class="k">def</span><span class="w"> </span><span class="nf">__not__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">_raise_NotImplementedError</span><span class="p">()</span> <span class="k">def</span><span class="w"> </span><span class="nf">__then__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">_raise_NotImplementedError</span><span class="p">()</span> <span class="k">def</span><span class="w"> </span><span class="nf">__else__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">ComparisonResultArray</span><span class="p">))</span> </pre></div> </div> <p>With this change, the chained comparison example above would be able to return:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mi">0</span> <span class="o">&lt;</span> <span class="n">increasing</span> <span class="o">&lt;</span> <span class="mi">4</span> <span class="go">ComparisonResultArray([ False, True, True, True, False], dtype=bool)</span> </pre></div> </div> </section> <section id="implementation"> <h2><a class="toc-backref" href="#implementation" role="doc-backlink">Implementation</a></h2> <p>Actual implementation has been deferred pending in-principle interest in the idea of making the changes proposed in <a class="pep reference internal" href="../pep-0532/" title="PEP 532 – A circuit breaking protocol and binary operators">PEP 532</a>.</p> <p>…TBD…</p> </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="id2" role="doc-footnote"> <dt class="label" id="id2">[<a href="#id1">1</a>]</dt> <dd>PEP 335 rejection notification (<a class="reference external" href="https://mail.python.org/pipermail/python-dev/2012-March/117510.html">https://mail.python.org/pipermail/python-dev/2012-March/117510.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 under the terms of the CC0 1.0 license: <a class="reference external" href="https://creativecommons.org/publicdomain/zero/1.0/">https://creativecommons.org/publicdomain/zero/1.0/</a></p> </section> </section> <hr class="docutils" /> <p>Source: <a class="reference external" href="https://github.com/python/peps/blob/main/peps/pep-0535.rst">https://github.com/python/peps/blob/main/peps/pep-0535.rst</a></p> <p>Last modified: <a class="reference external" href="https://github.com/python/peps/commits/main/peps/pep-0535.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="#pep-deferral">PEP Deferral</a></li> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#relationship-with-other-peps">Relationship with other PEPs</a></li> <li><a class="reference internal" href="#specification">Specification</a></li> <li><a class="reference internal" href="#rationale">Rationale</a></li> <li><a class="reference internal" href="#implementation">Implementation</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-0535.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