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: 11_further_object-oriented_features.html
+49-11Lines changed: 49 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,20 @@ <h1><span class="section-number">11. </span>Further object-oriented features<a c
65
65
in the course so far.</p>
66
66
<divclass="section" id="decorators">
67
67
<spanid="id1"></span><h2><spanclass="section-number">11.1. </span>Decorators<aclass="headerlink" href="#decorators" title="Permalink to this headline">¶</a></h2>
68
-
<p>In <aclass="reference internal" href="10_trees_and_directed_acyclic_graphs.html#trees"><spanclass="std std-numref">Week 10</span></a> we encountered the
<pclass="card-text">Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=f1d61410-4200-42e5-92c4-acf2011de8ab">watch this video on Panopto</a>.</p>
80
+
</div>
81
+
</details><p>In <aclass="reference internal" href="10_trees_and_directed_acyclic_graphs.html#trees"><spanclass="std std-numref">Week 10</span></a> we encountered the
69
82
<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> decorator, which turns a function into a
70
83
<aclass="reference internal" href="10_trees_and_directed_acyclic_graphs.html#term-single-dispatch-function"><spanclass="xref std std-term">single dispatch function</span></a>. More generally, a decorator is a function
71
84
which takes in a function and returns another function. In other words, the
<spanid="id2"></span><h2><spanclass="section-number">11.2. </span>Abstract base classes<aclass="headerlink" href="#abstract-base-classes" title="Permalink to this headline">¶</a></h2>
232
-
<p>We have now on several occasions encountered classes which are not designed to
245
+
<detailsclass="sphinx-bs dropdown card mb-3">
246
+
<summaryclass="summary-title card-header">
247
+
Video: Abstract base classes.<divclass="summary-down docutils">
<pclass="card-text">Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=f4678a69-731c-45fe-bdbf-acf2011de880">watch this video on Panopto</a>.</p>
257
+
</div>
258
+
</details><p>We have now on several occasions encountered classes which are not designed to
233
259
be instantiated themselves, but merely serve as parent classes to concrete
234
260
classes which are intended to be instantiated. Examples of these classes
235
261
include <aclass="reference external" href="https://docs.python.org/3/library/numbers.html#numbers.Number" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">numbers.Number</span></code></a>, <aclass="reference internal" href="example_code.html#example_code.groups.Group" title="example_code.groups.Group"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">example_code.groups.Group</span></code></a>, and the
<h3><spanclass="section-number">11.2.4. </span>Virtual subclasses<aclass="headerlink" href="#virtual-subclasses" title="Permalink to this headline">¶</a></h3>
429
-
<p>We learned in <aclass="reference internal" href="3_objects.html#objects"><spanclass="std std-numref">Week 3</span></a> that we can determine if a type is a
<pclass="card-text">Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=4114bb1d-cc31-4cfc-81a6-acf2011de8d6">watch this video on Panopto</a>.</p>
467
+
</div>
468
+
</details><p>We learned in <aclass="reference internal" href="3_objects.html#objects"><spanclass="std std-numref">Week 3</span></a> that we can determine if a type is a
430
469
number by checking if it is an instance of <aclass="reference external" href="https://docs.python.org/3/library/numbers.html#numbers.Number" title="(in Python v3.9)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">numbers.Number</span></code></a>. This is a
431
470
slightly different usage of abstract base classes. Rather than providing part
<h2><spanclass="section-number">11.4. </span>Exercises<aclass="headerlink" href="#exercises" title="Permalink to this headline">¶</a></h2>
616
-
<p>Obtain the <ahref="#id9"><spanclass="problematic" id="id10">`skeleton code for these exercises from GitHub classroom
617
-
<>`__</span></a>.</p>
655
+
<p>Obtain the <aclass="reference external" href="https://classroom.github.com/a/qTArFlxP">skeleton code for these exercises from GitHub classroom</a>.</p>
618
656
<divclass="proof proof-type-exercise" id="id7">
619
657
620
658
<divclass="proof-title">
@@ -629,11 +667,11 @@ <h2><span class="section-number">11.4. </span>Exercises<a class="headerlink" hre
629
667
taking any combination of arguments.</p>
630
668
<p>The logging itself should be accomplished using
631
669
the built-in <aclass="reference external" href="https://docs.python.org/3/library/logging.html#module-logging" title="(in Python v3.9)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">logging</span></code></a> module by calling <aclass="reference external" href="https://docs.python.org/3/library/logging.html#logging.info" title="(in Python v3.9)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">logging.info()</span></code></a> and passing
632
-
the logging message.</p>
633
-
<p>The logging message should comprise the function name (accessible using the
634
-
<aclass="reference external" href="https://docs.python.org/3/reference/import.html#__name__" title="(in Python v3.9)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">__name__</span></code></a> attribute), followed by round brackets containing first the
635
-
<aclass="reference external" href="https://docs.python.org/3/library/functions.html#repr" title="(in Python v3.9)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">repr()</span></code></a> of the positional arguments, followed by the key=value pairs
636
-
the keyword arguments.</p>
670
+
the log message.</p>
671
+
<p>The log message should comprise the string <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">"Calling:</span><spanclass="pre">"</span></code> followed by the
672
+
function name (accessible using the <aclass="reference external" href="https://docs.python.org/3/reference/import.html#__name__" title="(in Python v3.9)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">__name__</span></code></a> attribute), followed by
673
+
round brackets containing first the <aclass="reference external" href="https://docs.python.org/3/library/functions.html#repr" title="(in Python v3.9)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">repr()</span></code></a> of the positional
674
+
arguments, followed by the key=value pairs the keyword arguments.</p>
@@ -643,7 +681,7 @@ <h2><span class="section-number">11.4. </span>Exercises<a class="headerlink" hre
643
681
<p>The <codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">groups.groups</span></code> module in the skeleton code is the new version
644
682
introduced above, using an <aclass="reference internal" href="#term-abstract-base-class"><spanclass="xref std std-term">abstract base class</span></a>. The
645
683
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">log_decorator.log_call</span></code><aclass="reference internal" href="#term-decorator"><spanclass="xref std std-term">decorator</span></a> has been applied to the
646
-
<spanclass="math notranslate nohighlight">\(Group._validate\)</span><aclass="reference internal" href="#term-abstract-method"><spanclass="xref std std-term">abstract method</span></a>. However, even once you
684
+
<codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">Group._validate()</span></code><aclass="reference internal" href="#term-abstract-method"><spanclass="xref std std-term">abstract method</span></a>. However, even once you
647
685
have implemented this decorator, it never gets called. Your challenge is to
648
686
modify <codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">groups.groups</span></code> so that the decorator is called every time a
649
687
subclass <codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">_validate()</span></code> method is called, but <strong>without</strong> moving or
0 commit comments