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
</iframe></div><p>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>
78
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=75d2f2a5-5ab9-464b-8eee-ae1c00dbf366">watch this video on Panopto</a>.</p>
79
79
</details><p>In <aclass="reference internal" href="9_trees_and_directed_acyclic_graphs.html#trees"><spanclass="std std-numref">Chapter 9</span></a> we encountered the
80
80
<aclass="reference external" href="https://docs.python.org/3/library/functools.html#functools.singledispatch" title="(in Python v3.11)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">functools.singledispatch()</span></code></a> decorator, which turns a function into a
81
81
<aclass="reference internal" href="9_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
</iframe></div><p>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>
238
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=9a9b872a-9499-40b7-a228-ae1c00dbf947">watch this video on Panopto</a>.</p>
239
239
</details><p>We have now on several occasions encountered classes which are not designed to
240
240
be instantiated themselves, but merely serve as parent classes to concrete
241
241
classes which are intended to be instantiated. Examples of these classes
</iframe></div><p>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>
397
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=fd48b1c1-8e81-4c6d-8415-ae1c00dc07eb">watch this video on Panopto</a>.</p>
398
398
</details><p>We learned in <aclass="reference internal" href="3_objects.html#objects"><spanclass="std std-numref">Chapter 3</span></a> that we can determine if a type is a
399
399
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.11)"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">numbers.Number</span></code></a>. This is a
400
400
slightly different usage of abstract base classes. Rather than providing part
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=011d73de-d93c-4dc8-8996-ac8501521b33">watch this video on Panopto</a></p>
162
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=d63e1c6e-18ff-48de-af03-ae1c00da3b3f">watch this video on Panopto</a></p>
163
163
</details><p>In the course of the exercises, You’re going to create, edit, and install a
164
164
whole bunch of Python packages. It’s highly desirable have a predictable
165
165
programming environment in which the experiments you’re doing don’t interfere
@@ -298,7 +298,7 @@ <h3><span class="section-number">1.2.2. </span>Creating the venv<a class="header
298
298
<h2><spanclass="section-number">1.3. </span>Installing Python packages<aclass="headerlink" href="#installing-python-packages" title="Permalink to this headline">¶</a></h2>
299
299
<p>Suppose we’ve created and activated a venv, and now there’s a Python package
300
300
we’d like to have access to. Installation of Python packages is handled by the
301
-
Python package <aclass="reference external" href="https://pip.pypa.io/en/stable/" title="(in pip v22.3)"><spanclass="xref std std-doc">Pip</span></a>, which you will usually find
301
+
Python package <aclass="reference external" href="https://pip.pypa.io/en/stable/" title="(in pip v23.0)"><spanclass="xref std std-doc">Pip</span></a>, which you will usually find
302
302
pre-installed in your Python installation. Pip has many usage options, which
303
303
enable a large number of different installation configurations. However, for
304
304
most users most of the time, a few simple pip commands suffice. As with
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=0f9a50a0-59b4-4bdf-ab90-ac850154fafb">watch this video on Panopto</a></p>
87
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=8773e5b7-a331-4ca3-a59d-ae1c00da3b4f">watch this video on Panopto</a></p>
88
88
</details><p>Before we dive into the various different ways that Python code can be
89
89
organised and run, it’s helpful to have a mental model of what it actually
90
90
means for Python code to execute. Python is an interpreted language. This means
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=972f92c6-6b55-4510-9c2c-ac8600fca11a">watch this video on Panopto</a></p>
276
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=2f0cb956-9e78-4022-94ff-ae1c00da3b41">watch this video on Panopto</a></p>
270
277
</details><p>A module is, like a script, a plain text file containing Python
271
278
code. Modules must have names ending in <codeclass="file docutils literal notranslate"><spanclass="pre">.py</span></code>. So far, that’s
272
279
identical to a script. Indeed, it’s sometimes possible (though not
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=c4b0aedd-02a8-45d1-946b-ac86015b6d0b">watch this video on Panopto</a></p>
413
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=a1a9c7ab-147b-491d-b3dc-ae1c00da3b3f">watch this video on Panopto</a></p>
407
414
</details><p>Modules are the principal mechanism for storing code which is intended
408
415
to be used by other code. However, putting all of the code for a
409
416
complex area of mathematics in a single huge Python file is not a
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=c636383d-6125-4a7c-bad7-ac86015b6d4c">watch this video on Panopto</a></p>
670
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=73421f63-998c-4273-9c41-ae1c00da4624">watch this video on Panopto</a></p>
664
671
</details><p>Attempting to establish whether a program correctly implements the intended
665
672
algorithm is core to effective programming, and programmers often spend more
666
673
time correcting bugs than writing new code. We will turn to the question of how
<p>There are a number of Python packages which support code testing. The concepts
671
678
are largely similar so rather than get bogged down in the details of multiple
672
-
frameworks, we will introduce <aclass="reference external" href="https://docs.pytest.org/en/latest/index.html" title="(in pytest v0.1.dev435+g3ad4344)"><spanclass="xref std std-doc">Pytest</span></a>, which is one of the
679
+
frameworks, we will introduce <aclass="reference external" href="https://docs.pytest.org/en/latest/index.html" title="(in pytest v7.4.0.dev8+g22524046c)"><spanclass="xref std std-doc">Pytest</span></a>, which is one of the
673
680
most widely used. Pytest is simply a Python package, so you can install it into
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=f8b07554-16ea-47b8-bf19-ac8a010af0f6">watch this video on Panopto</a></p>
231
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=b70b944d-612c-4e57-9538-af8d01031342">watch this video on Panopto</a></p>
232
232
</details><p>Python has a rich set of <aclass="reference external" href="https://docs.python.org/3/library/stdtypes.html" title="(in Python v3.11)"><spanclass="xref std std-doc">built-in types</span></a>. These form powerful building blocks for the
233
233
language, but one very soon finds mathematical abstractions which do
234
234
not have implementations among the built-in types of the Python
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=613249ca-71b8-4f3b-8db9-ac8a0166aa42">watch this video on Panopto</a></p>
354
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=b3a56e5f-2ed4-4417-aaf8-ae1c00da6017">watch this video on Panopto</a></p>
355
355
</details><p>We have already met the <aclass="reference internal" href="#term-special-method"><spanclass="xref std std-term">special method</span></a><aclass="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__init__" title="(in Python v3.11)"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__init__()</span></code></a>,
356
356
which defines the class constructor. A much more typical case is an
357
357
ordinary method, without a special underscore name. For example,
@@ -401,7 +401,7 @@ <h3><span class="section-number">3.3.4. </span>String representations of objects
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=b30db20c-1224-41b7-a5f2-ac8a01680608">watch this video on Panopto</a></p>
404
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=4fe0252a-619d-4959-99f0-ae1c00da657d">watch this video on Panopto</a></p>
405
405
</details><p>Remember that a key reason for defining new classes is to enable users
406
406
to reason about the resulting objects at a higher mathematical level. An
407
407
important aid to the user in doing this is to be able to look at the
@@ -496,7 +496,7 @@ <h3><span class="section-number">3.3.4. </span>String representations of objects
496
496
<summary>
497
497
Video: object equality and test driven development</summary><divclass="video_wrapper" style="">
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=821e53ec-d2c8-43a6-bb16-ac8c01045f31">watch this video on Panopto</a></p>
499
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=17aa405c-bf70-4007-b365-ae1c00da7976">watch this video on Panopto</a></p>
500
500
</details><p>When are two objects equal? For built-in types Python has equality rules which
501
501
broadly match the mathematical identities that you might expect. For example,
502
502
two numbers of different types are equal if their numerical value is equal:</p>
@@ -576,7 +576,7 @@ <h3><span class="section-number">3.3.4. </span>String representations of objects
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=727c5b9a-bf61-480e-912e-ac8c01045f09">watch this video on Panopto</a></p>
579
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=d9a38b7e-d182-46f6-90fc-ae1c00da8682">watch this video on Panopto</a></p>
580
580
</details><p>It’s all very well to be able to compare our polynomial objects, but
581
581
we won’t really have captured the mathematical abstraction involved
582
582
unless we have at least some mathematical operations. We have already
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=3572d8ea-5635-4b1a-9243-acae0150ddf8">watch this video on Panopto</a></p>
74
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=81bda525-60bb-46a7-bdc9-ae1c00da8a74">watch this video on Panopto</a></p>
75
75
</details><sectionid="the-value-of-convention">
76
76
<h2><spanclass="section-number">4.1. </span>The value of convention<aclass="headerlink" href="#the-value-of-convention" title="Permalink to this headline">¶</a></h2>
77
77
<p>Consider the following definition of the limit of a function <spanclass="math notranslate nohighlight">\(f\)</span> at a
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=91c271e4-a61f-493c-a725-acae015273d5">watch this video on Panopto</a></p>
161
+
</iframe></div><p>Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=63c09ddd-ec18-4746-b1b9-ae1c00da9680">watch this video on Panopto</a></p>
162
162
</details><p>One of the helpful characteristics of PEP 8 is that many of its strictures can
163
163
be enforced automatically by a computer program. Programs that automatically
164
164
check programming style are called linters. Lint are the little fibres that tend
errors. However, it’s also likely to be the case that there are some rules that
239
239
you just don’t want to apply across your whole project. This configuration can
240
240
be achieved in the file <codeclass="file docutils literal notranslate"><spanclass="pre">setup.cfg</span></code>, which lives in the top of your
241
-
Git repository alongside <codeclass="file docutils literal notranslate"><spanclass="pre">setup.py</span></code>. <codeclass="file docutils literal notranslate"><spanclass="pre">setup.cfg</span></code> can be
241
+
Git repository alongside <codeclass="file docutils literal notranslate"><spanclass="pre">pyproject.toml</span></code>. <codeclass="file docutils literal notranslate"><spanclass="pre">setup.cfg</span></code> can be
242
242
used to configure a lot of different Python tools, so it stores different
243
243
configurations in different sections. <aclass="reference internal" href="#flake8conf"><spanclass="std std-numref">Listing 4.1</span></a> shows an example
244
244
Flake8 section. The full list of available options is presented in the <aclass="reference external" href="https://flake8.pycqa.org">Flake8
0 commit comments