From 036b94e66872049acdb2791a14192e5438bece6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= <sornjure@gmail.com> Date: Tue, 13 May 2025 00:05:00 +0200 Subject: [PATCH] Inheritence to Subclass in Class, Paths, CSV --- README.md | 32 +++++++++++++++++--------------- index.html | 35 ++++++++++++++++++++--------------- pdf/index_for_pdf.html | 2 +- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index c906e61..4f2da95 100644 --- a/README.md +++ b/README.md @@ -995,29 +995,31 @@ Z = make_dataclass('Z', ['a']); print/str/repr(Z(<obj>)) >>> <obj> ``` -### Inheritance +### Subclass +* **Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.** +* **Subclass can then add its own methods and attributes or override inherited ones by reusing their names.** + ```python class Person: def __init__(self, name): self.name = name + def __repr__(self): + return f'Person({self.name!r})' + def __lt__(self, other): + return self.name < other.name class Employee(Person): def __init__(self, name, staff_num): super().__init__(name) self.staff_num = staff_num + def __repr__(self): + return f'Employee({self.name!r}, {self.staff_num})' ``` -#### Multiple inheritance: -```python -class A: pass -class B: pass -class C(A, B): pass -``` - -**MRO determines the order in which parent classes are traversed when searching for a method or an attribute:** ```python ->>> C.mro() -[<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>] +>>> people = {Person('Ann'), Employee('Bob', 0)} +>>> sorted(people) +[Person('Ann'), Employee('Bob', 0)] ``` ### Type Annotations @@ -1685,8 +1687,8 @@ from pathlib import Path <Path> = <Path>.parent # Returns Path without the final component. <str> = <Path>.name # Returns final component as a string. <str> = <Path>.suffix # Returns name's last extension, e.g. '.py'. -<str> = <Path>.stem # Returns name without last extension. -<tup.> = <Path>.parts # Returns all path's components as strings. +<str> = <Path>.stem # Returns name without the last extension. +<tup.> = <Path>.parts # Returns all components as strings. ``` ```python @@ -1820,10 +1822,10 @@ import csv <list> = next(<reader>) # Returns next row as a list of strings. <list> = list(<reader>) # Returns a list of remaining rows. ``` -* **File must be opened with a `'newline=""'` argument, or newlines embedded inside quoted fields will not be interpreted correctly!** +* **File must be opened with a `'newline=""'` argument, or all '\r\n' sequences inside quoted fields will get converted to '\n'!** * **To print the spreadsheet to the console use [Tabulate](#table) library.** * **For XML and binary Excel files (xlsx, xlsm and xlsb) use [Pandas](#dataframe-plot-encode-decode) library.** -* **Reader accepts any iterator of strings, not just files.** +* **Reader accepts any collection of strings, not just files.** ### Write ```python diff --git a/index.html b/index.html index d2af9dd..a26d191 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@ <body> <header> - <aside>May 9, 2025</aside> + <aside>May 12, 2025</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </header> @@ -849,25 +849,30 @@ Z = make_dataclass(<span class="hljs-string">'Z'</span>, [<span class="hljs-stri <span class="hljs-meta">>>> </span><obj> </code></pre></div> -<div><h3 id="inheritance">Inheritance</h3><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span> +<div><h3 id="subclass">Subclass</h3><ul> +<li><strong>Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.</strong></li> +<li><strong>Subclass can then add its own methods and attributes or override inherited ones by reusing their names.</strong></li> +</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span> self.name = name + <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span> + <span class="hljs-keyword">return</span> <span class="hljs-string">f'Person(<span class="hljs-subst">{self.name!r}</span>)'</span> + <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__lt__</span><span class="hljs-params">(self, other)</span>:</span> + <span class="hljs-keyword">return</span> self.name < other.name <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Employee</span><span class="hljs-params">(Person)</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, staff_num)</span>:</span> super().__init__(name) self.staff_num = staff_num + <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span> + <span class="hljs-keyword">return</span> <span class="hljs-string">f'Employee(<span class="hljs-subst">{self.name!r}</span>, <span class="hljs-subst">{self.staff_num}</span>)'</span> </code></pre></div> -<div><h4 id="multipleinheritance">Multiple inheritance:</h4><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span>:</span> <span class="hljs-keyword">pass</span> -<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span>:</span> <span class="hljs-keyword">pass</span> -<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">C</span><span class="hljs-params">(A, B)</span>:</span> <span class="hljs-keyword">pass</span> -</code></pre></div> -<p><strong>MRO determines the order in which parent classes are traversed when searching for a method or an attribute:</strong></p> -<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>C.mro() -[<<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">C</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">A</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">B</span>'>, <<span class="hljs-title">class</span> '<span class="hljs-title">object</span>'>] -</span></code></pre> +<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>people = {Person(<span class="hljs-string">'Ann'</span>), Employee(<span class="hljs-string">'Bob'</span>, <span class="hljs-number">0</span>)} +<span class="hljs-meta">>>> </span>sorted(people) +[Person(<span class="hljs-string">'Ann'</span>), Employee(<span class="hljs-string">'Bob'</span>, <span class="hljs-number">0</span>)] +</code></pre> <div><h3 id="typeannotations">Type Annotations</h3><ul> <li><strong>They add type hints to variables, arguments and functions (<code class="python hljs"><span class="hljs-string">'def f() -> <type>:'</span></code>).</strong></li> <li><strong>Hints are used by type checkers like <a href="https://pypi.org/project/mypy/">mypy</a>, data validation libraries such as <a href="https://pypi.org/project/pydantic/">Pydantic</a> and lately also by <a href="https://pypi.org/project/Cython/">Cython</a> compiler. However, they are not enforced by CPython interpreter.</strong></li> @@ -1431,8 +1436,8 @@ args = p.parse_args() <span class="h <pre><code class="python language-python hljs"><Path> = <Path>.parent <span class="hljs-comment"># Returns Path without the final component.</span> <str> = <Path>.name <span class="hljs-comment"># Returns final component as a string.</span> <str> = <Path>.suffix <span class="hljs-comment"># Returns name's last extension, e.g. '.py'.</span> -<str> = <Path>.stem <span class="hljs-comment"># Returns name without last extension.</span> -<tup.> = <Path>.parts <span class="hljs-comment"># Returns all path's components as strings.</span> +<str> = <Path>.stem <span class="hljs-comment"># Returns name without the last extension.</span> +<tup.> = <Path>.parts <span class="hljs-comment"># Returns all components as strings.</span> </code></pre> <pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span> <iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span> @@ -1522,10 +1527,10 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs- </code></pre></div> <ul> -<li><strong>File must be opened with a <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or newlines embedded inside quoted fields will not be interpreted correctly!</strong></li> +<li><strong>File must be opened with a <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or all '\r\n' sequences inside quoted fields will get converted to '\n'!</strong></li> <li><strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> library.</strong></li> <li><strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#dataframeplotencodedecode">Pandas</a> library.</strong></li> -<li><strong>Reader accepts any iterator of strings, not just files.</strong></li> +<li><strong>Reader accepts any collection of strings, not just files.</strong></li> </ul> <div><h3 id="write">Write</h3><pre><code class="python language-python hljs"><writer> = csv.writer(<file>) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span> <writer>.writerow(<collection>) <span class="hljs-comment"># Encodes objects using `str(<el>)`.</span> @@ -2940,7 +2945,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active <footer> - <aside>May 9, 2025</aside> + <aside>May 12, 2025</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </footer> diff --git a/pdf/index_for_pdf.html b/pdf/index_for_pdf.html index 9cb299e..4327865 100644 --- a/pdf/index_for_pdf.html +++ b/pdf/index_for_pdf.html @@ -131,7 +131,7 @@ <strong>strings, <a href="#abstractbaseclasses">4</a>-<a href="#comparisonofpresentationtypes">7</a>, <a href="#class">14</a></strong><br> <strong>struct module, <a href="#struct">28</a>-<a href="#integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">29</a></strong><br> <strong>subprocess module, <a href="#sends11tothebasiccalculatorandcapturesitsoutput">25</a></strong><br> -<strong>super function, <a href="#inheritance">14</a></strong><br> +<strong>super function, <a href="#subclass">14</a></strong><br> <strong>sys module, <a href="#cache">13</a>, <a href="#exit">21</a>-<a href="#commandlinearguments">22</a></strong> </p> <h3 id="t">T</h3> <p><strong>table, <a href="#csv">26</a>, <a href="#example-1">27</a>, <a href="#table">34</a>, <a href="#numpy">37</a>-<a href="#indexing">38</a>, <a href="#dataframe">45</a>-<a href="#dataframeaggregatetransformmap">46</a></strong><br>