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
Copy file name to clipboardExpand all lines: 10_trees_and_directed_acyclic_graphs.html
+15-18Lines changed: 15 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -440,9 +440,9 @@ <h3><span class="section-number">10.2.2. </span>Traversing <a class="reference i
440
440
<p>This time the visitor <aclass="reference external" href="https://docs.python.org/3/library/functions.html#sum" title="(in Python v3.9)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">sums</span></code></a> the results from its children, and adds
441
441
one for itself.</p>
442
442
<p>What about preorder traversal? This time we need a little more code (not much)
443
-
as <codeclass="xref std std-numref docutils literal notranslate"><spanclass="pre">preorder_recursive</span></code> shows.</p>
443
+
as <aclass="reference internal" href="#preorder-recursive"><spanclass="std std-numref">Listing 10.5</span></a> shows.</p>
@@ -505,7 +505,7 @@ <h3><span class="section-number">10.2.2. </span>Traversing <a class="reference i
505
505
</div>
506
506
</div>
507
507
<divclass="section" id="expression-trees">
508
-
<h2><spanclass="section-number">10.3. </span>Expression trees<aclass="headerlink" href="#expression-trees" title="Permalink to this headline">¶</a></h2>
508
+
<spanid="expr-trees"></span><h2><spanclass="section-number">10.3. </span>Expression trees<aclass="headerlink" href="#expression-trees" title="Permalink to this headline">¶</a></h2>
509
509
<p>One important application of trees is in representing arithmetic expressions.
510
510
Consider the expression <spanclass="math notranslate nohighlight">\(2y + 4^{(5 + x)}\)</span>. Suppose, further, that
511
511
we want to represent this on a computer in such a way that we can perform
is a pair of round brackets containing the string representation of each
624
624
item in the tuple.</p>
625
625
</dd>
626
-
<dt><aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__str__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__str__()</span></code></a></dt><dd><p>This is the human-readable string output, using <spanclass="xref std std-term">infix</span> operators, so
627
-
in the example above we would expect to see <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">2</span><spanclass="pre">*</span><spanclass="pre">y</span><spanclass="pre">+</span><spanclass="pre">4</span><spanclass="pre">^</span><spanclass="pre">(5</span><spanclass="pre">+</span><spanclass="pre">x)</span></code> This looks
628
-
sort of straightforward, simply associate the correct symbol with each
629
-
operator class as a <aclass="reference internal" href="8_inheritance.html#term-class-attribute"><spanclass="xref std std-term">class attribute</span></a> and place the string
630
-
representation of the operands on either side.</p>
626
+
<dt><aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__str__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__str__()</span></code></a></dt><dd><p>This is the human-readable string output, using <aclass="reference internal" href="3_objects.html#term-infix-operator"><spanclass="xref std std-term">infix operators</span></a>, so in the example above we would expect to see
627
+
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">2</span><spanclass="pre">*</span><spanclass="pre">y</span><spanclass="pre">+</span><spanclass="pre">4</span><spanclass="pre">^</span><spanclass="pre">(5</span><spanclass="pre">+</span><spanclass="pre">x)</span></code> This looks sort of straightforward, simply associate the correct
628
+
symbol with each operator class as a <aclass="reference internal" href="8_inheritance.html#term-class-attribute"><spanclass="xref std std-term">class attribute</span></a> and place the
629
+
string representation of the operands on either side.</p>
631
630
<p>The challenge is to correctly include the brackets. In order to do this, it
632
631
is necessary to associate with every expression class a <aclass="reference internal" href="8_inheritance.html#term-class-attribute"><spanclass="xref std std-term">class
633
632
attribute</span></a> whose value is a number giving an operator precedence to that
<p>Individual operator classes therefore need to define very little, just two
642
-
<spanclass="xref std std-term">class attributes</span>, one for the operator precedence, and one to set the
643
-
symbol when printing the operator.</p>
641
+
<aclass="reference internal" href="8_inheritance.html#term-class-attribute"><spanclass="xref std std-term">class attributes</span></a>, one for the operator precedence, and
642
+
one to set the symbol when printing the operator.</p>
644
643
<p>Let’s now consider <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Terminal</span></code>. What does it need to set?</p>
645
644
<dlclass="simple">
646
645
<dt><aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__init__" title="(in Python v3.9)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__init__()</span></code></a></dt><dd><p>The <aclass="reference internal" href="3_objects.html#term-constructor"><spanclass="xref std std-term">constructor</span></a> for <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Expression</span></code> assumes that an expression is
647
646
defined by a series of operands. Terminals have an empty list of operands
648
647
but do have something that other expressions lack, a value. In the case of
649
-
<spanclass="xref std std-term">Number</span>, this is a number, while for <spanclass="xref std std-term">Symbol</span> the value is a
648
+
<codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Number</span></code>, this is a number, while for <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Symbol</span></code> the value is a
650
649
string (usually a single character). <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Terminal</span></code> therefore needs its
651
650
own <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__init__()</span></code> which will take a value argument.
652
651
<codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Terminal.__init__</span></code> still has to call the <aclass="reference internal" href="8_inheritance.html#term-superclass"><spanclass="xref std std-term">superclass</span></a>
<aclass="reference external" href="https://docs.python.org/3/library/functools.html#functools.singledispatch" title="(in Python v3.9)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">functools.singledispatch()</span></code></a> turns a function into
824
823
a single dispatch function. The <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">@</span></code> symbol marks
825
824
<aclass="reference external" href="https://docs.python.org/3/library/functools.html#functools.singledispatch" title="(in Python v3.9)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">singledispatch()</span></code></a> as a <aclass="reference external" href="https://docs.python.org/3/glossary.html#term-decorator" title="(in Python v3.9)"><spanclass="xref std std-term">decorator</span></a>. We’ll return to them
826
-
in <codeclass="xref std std-numref docutils literal notranslate"><spanclass="pre">decorators</span></code>. For the moment, we just need to know that
825
+
in <aclass="reference internal" href="11_further_object-oriented_features.html#decorators"><spanclass="std std-numref">Section 11.1</span></a>. For the moment, we just need to know that
827
826
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">@singledispatch</span></code> turns the function it precedes into a single dispatch
828
827
function.</p>
829
828
<p>Next we turn our attention to the implementation of evaluation for the different
discussed above. Instead, we can construct a postorder DAG visitor using a
1035
1034
<aclass="reference internal" href="5_abstract_data_types.html#term-stack"><spanclass="xref std std-term">stack</span></a> to replace the recursion in keeping track of what to evaluate
1036
1035
next, and a <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">dict</span></code></a> to record the nodes we have already visited, and the
1037
-
results of visiting them. <codeclass="xref std std-numref docutils literal notranslate"><spanclass="pre">nonrecursive_postvisit</span></code> illustrates one such
1036
+
results of visiting them. <aclass="reference internal" href="#nonrecursive-postvisit"><spanclass="std std-numref">Listing 10.8</span></a> illustrates one such
<divclass="code-block-caption"><spanclass="caption-number">Listing 10.8 </span><spanclass="caption-text">Pythonic pseudocode for a non-recursive postorder <aclass="reference internal" href="#term-DAG"><spanclass="xref std std-term">DAG</span></a> visitor.</span><aclass="headerlink" href="#id23" title="Permalink to this code">¶</a></div>
1039
+
<spanid="nonrecursive-postvisit"></span><divclass="code-block-caption"><spanclass="caption-number">Listing 10.8 </span><spanclass="caption-text">Pythonic pseudocode for a non-recursive postorder <aclass="reference internal" href="#term-DAG"><spanclass="xref std std-term">DAG</span></a> visitor.</span><aclass="headerlink" href="#id23" title="Permalink to this code">¶</a></div>
<h2><spanclass="section-number">10.6. </span>Differentiation as an expression visitor<aclass="headerlink" href="#differentiation-as-an-expression-visitor" title="Permalink to this headline">¶</a></h2>
1101
-
<p>In <codeclass="xref std std-numref docutils literal notranslate"><spanclass="pre">expr_trees</span></code> we showed how a tree visitor could implement the
1100
+
<p>In <aclass="reference internal" href="#expr-trees"><spanclass="std std-numref">Section 10.3</span></a> we showed how a tree visitor could implement the
1102
1101
evaluation of a symbolic expression. You might very well protest that if the
1103
1102
only thing you wanted to do was evaluate arithmetic expressions then you could
1104
1103
have just written a Python function and avoided a lot of code. In fact, almost
@@ -1134,8 +1133,7 @@ <h2><span class="section-number">10.6. </span>Differentiation as an expression v
1134
1133
</div>
1135
1134
<divclass="section" id="glossary">
1136
1135
<h2><spanclass="section-number">10.7. </span>Glossary<aclass="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h2>
1137
-
<blockquote>
1138
-
<div><dlclass="glossary simple">
1136
+
<dlclass="glossary simple">
1139
1137
<dtid="term-child-node">child node</dt><dd><p>The children of a node in a <aclass="reference internal" href="#term-DAG"><spanclass="xref std std-term">DAG</span></a> are the targets of the edges
1140
1138
emerging from that node. In this context, the node from which these
1141
1139
edges emanate is called the <aclass="reference internal" href="#term-parent-node"><spanclass="xref std std-term">parent node</span></a>.</p>
<h1><spanclass="section-number">11. </span>Further object-oriented features<aclass="headerlink" href="#further-object-oriented-features" title="Permalink to this headline">¶</a></h1>
56
56
<divclass="section" id="decorators">
57
-
<h2><spanclass="section-number">11.1. </span>Decorators<aclass="headerlink" href="#decorators" title="Permalink to this headline">¶</a></h2>
57
+
<spanid="id1"></span><h2><spanclass="section-number">11.1. </span>Decorators<aclass="headerlink" href="#decorators" title="Permalink to this headline">¶</a></h2>
58
58
</div>
59
59
<divclass="section" id="abstract-classes">
60
-
<h2><spanclass="section-number">11.2. </span>Abstract classes<aclass="headerlink" href="#abstract-classes" title="Permalink to this headline">¶</a></h2>
60
+
<spanid="id2"></span><h2><spanclass="section-number">11.2. </span>Abstract classes<aclass="headerlink" href="#abstract-classes" title="Permalink to this headline">¶</a></h2>
61
61
</div>
62
62
<divclass="section" id="abstract-base-classes">
63
-
<spanid="id1"></span><h2><spanclass="section-number">11.3. </span>Abstract base classes<aclass="headerlink" href="#abstract-base-classes" title="Permalink to this headline">¶</a></h2>
63
+
<spanid="id3"></span><h2><spanclass="section-number">11.3. </span>Abstract base classes<aclass="headerlink" href="#abstract-base-classes" title="Permalink to this headline">¶</a></h2>
64
+
<divclass="section" id="glossary">
65
+
<h3><spanclass="section-number">11.3.1. </span>Glossary<aclass="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h3>
66
+
<dlclass="glossary simple">
67
+
<dtid="term-abstract-class">abstract class</dt><dd><p>A class designed only to be the <aclass="reference internal" href="8_inheritance.html#term-parent-class"><spanclass="xref std std-term">parent</span></a> of other
68
+
classes, and never to be instantiated itself. Abstract classes often
69
+
define the interfaces of <aclass="reference internal" href="3_objects.html#term-method"><spanclass="xref std std-term">methods</span></a> but leave their implementations
70
+
to the concrete <aclass="reference internal" href="8_inheritance.html#term-child-class"><spanclass="xref std std-term">child classes</span></a>.</p>
0 commit comments