You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h3><spanclass="section-number">4.5.1. </span>Use comprehensions<aclass="headerlink" href="#use-comprehensions" title="Permalink to this headline">¶</a></h3>
377
393
<p>It is very common to write loops to populate collection objects with
<p>This is a fairly typical, if simple, example. It takes three lines of
388
404
code: one to initialise the list, one to loop, and one to add the
389
-
values to the list. Alternatively, if we had used a <aclass="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-listcomps" title="(in Python v3.8)"><spanclass="xref std std-ref">list
405
+
values to the list. Alternatively, if we had used a <aclass="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-listcomps" title="(in Python v3.9)"><spanclass="xref std std-ref">list
390
406
comprehension</span></a>, all three steps would have been subsumed into a single
<p>At least for fairly simple operations, comprehensions are almost
398
414
always easier for the reader to understand than loops. In addition to
399
-
lists, comprehensions are also available for <aclass="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-sets" title="(in Python v3.8)"><spanclass="xref std std-ref">sets</span></a>
400
-
and <aclass="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-dictionaries" title="(in Python v3.8)"><spanclass="xref std std-ref">dictionaries</span></a>.</p>
415
+
lists, comprehensions are also available for <aclass="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-sets" title="(in Python v3.9)"><spanclass="xref std std-ref">sets</span></a>
416
+
and <aclass="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-dictionaries" title="(in Python v3.9)"><spanclass="xref std std-ref">dictionaries</span></a>.</p>
<h3><spanclass="section-number">4.5.2. </span>Redundant logical expressions<aclass="headerlink" href="#redundant-logical-expressions" title="Permalink to this headline">¶</a></h3>
@@ -408,7 +424,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
408
424
</div>
409
425
</div>
410
426
<p>To see the problem with this statement, let’s write out its truth table:</p>
@@ -428,7 +444,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
428
444
</tbody>
429
445
</table>
430
446
<p>In other words, the expressions <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">var</span></code> and <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">var</span><spanclass="pre">==</span><spanclass="pre">True</span></code> are logically
431
-
equivalent (at least assuming <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">var</span></code> is a <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values" title="(in Python v3.8)"><spanclass="xref std std-ref">boolean value</span></a>), so it would
447
+
equivalent (at least assuming <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">var</span></code> is a <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values" title="(in Python v3.9)"><spanclass="xref std std-ref">boolean value</span></a>), so it would
@@ -447,7 +463,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
447
463
</pre></div>
448
464
</div>
449
465
</div>
450
-
<p>Finally, the use of <aclass="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#else" title="(in Python v3.8)"><spanclass="xref std std-ref">else</span></a> (or <aclass="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#elif" title="(in Python v3.8)"><spanclass="xref std std-ref">elif</span></a>) can reduce the number
466
+
<p>Finally, the use of <aclass="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#else" title="(in Python v3.9)"><spanclass="xref std std-ref">else</span></a> (or <aclass="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#elif" title="(in Python v3.9)"><spanclass="xref std std-ref">elif</span></a>) can reduce the number
451
467
of logical expressions that the reader has to read and
452
468
understand. This means that:</p>
453
469
<divclass="badcode docutils container">
@@ -474,7 +490,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
<h3><spanclass="section-number">4.5.3. </span>Use the fact that every object is True or False<aclass="headerlink" href="#use-the-fact-that-every-object-is-true-or-false" title="Permalink to this headline">¶</a></h3>
477
-
<p>Every Python object is logically either <aclass="reference external" href="https://docs.python.org/3/library/constants.html#True" title="(in Python v3.8)"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">True</span></code></a> or <aclass="reference external" href="https://docs.python.org/3/library/constants.html#False" title="(in Python v3.8)"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">False</span></code></a> according to the
493
+
<p>Every Python object is logically either <aclass="reference external" href="https://docs.python.org/3/library/constants.html#True" title="(in Python v3.9)"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">True</span></code></a> or <aclass="reference external" href="https://docs.python.org/3/library/constants.html#False" title="(in Python v3.9)"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">False</span></code></a> according to the
478
494
following rules:</p>
479
495
<olclass="arabic simple">
480
496
<li><p>None is False.</p></li>
@@ -484,16 +500,16 @@ <h3><span class="section-number">4.5.3. </span>Use the fact that every object is
484
500
<li><p>The null string <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">""</span></code> is False, a string containing any characters is True.</p></li>
method</span></a>. In this case the truth value is whatever this method
489
505
returns.</p></li>
490
-
<li><p>It doesn’t define <aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" title="(in Python v3.8)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__bool__()</span></code></a> but does define
491
-
<aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__len__" title="(in Python v3.8)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__len__()</span></code></a>. In this case the object is False if the
506
+
<li><p>It doesn’t define <aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__bool__()</span></code></a> but does define
507
+
<aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__len__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__len__()</span></code></a>. In this case the object is False if the
492
508
length is zero, and True otherwise.</p></li>
493
509
</ol>
494
510
</li>
495
511
</ol>
496
-
<p>These rules are laid out formally in <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#truth" title="(in Python v3.8)"><spanclass="xref std std-ref">the Python documentation</span></a>. One way that they can be used to write simpler, clearer code
512
+
<p>These rules are laid out formally in <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#truth" title="(in Python v3.9)"><spanclass="xref std std-ref">the Python documentation</span></a>. One way that they can be used to write simpler, clearer code
497
513
is in the very common case of code that should only execute if a
498
514
collection object actually contains something. In that case, this form
0 commit comments