|
56 | 56 |
|
57 | 57 | <body>
|
58 | 58 | <header>
|
59 |
| - <aside>May 9, 2025</aside> |
| 59 | + <aside>May 12, 2025</aside> |
60 | 60 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
61 | 61 | </header>
|
62 | 62 |
|
|
849 | 849 | <span class="hljs-meta">>>> </span><obj>
|
850 | 850 | </code></pre></div>
|
851 | 851 |
|
852 |
| -<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> |
| 852 | +<div><h3 id="subclass">Subclass</h3><ul> |
| 853 | +<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> |
| 854 | +<li><strong>Subclass can then add its own methods and attributes or override inherited ones by reusing their names.</strong></li> |
| 855 | +</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> |
853 | 856 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span>
|
854 | 857 | self.name = name
|
| 858 | + <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span> |
| 859 | + <span class="hljs-keyword">return</span> <span class="hljs-string">f'Person(<span class="hljs-subst">{self.name!r}</span>)'</span> |
| 860 | + <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__lt__</span><span class="hljs-params">(self, other)</span>:</span> |
| 861 | + <span class="hljs-keyword">return</span> self.name < other.name |
855 | 862 |
|
856 | 863 | <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Employee</span><span class="hljs-params">(Person)</span>:</span>
|
857 | 864 | <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>
|
858 | 865 | super().__init__(name)
|
859 | 866 | self.staff_num = staff_num
|
| 867 | + <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span> |
| 868 | + <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> |
860 | 869 | </code></pre></div>
|
861 | 870 |
|
862 |
| -<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> |
863 |
| -<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span>:</span> <span class="hljs-keyword">pass</span> |
864 |
| -<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> |
865 |
| -</code></pre></div> |
866 | 871 |
|
867 |
| -<p><strong>MRO determines the order in which parent classes are traversed when searching for a method or an attribute:</strong></p> |
868 |
| -<pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>C.mro() |
869 |
| -[<<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>'>] |
870 |
| -</span></code></pre> |
| 872 | +<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>)} |
| 873 | +<span class="hljs-meta">>>> </span>sorted(people) |
| 874 | +[Person(<span class="hljs-string">'Ann'</span>), Employee(<span class="hljs-string">'Bob'</span>, <span class="hljs-number">0</span>)] |
| 875 | +</code></pre> |
871 | 876 | <div><h3 id="typeannotations">Type Annotations</h3><ul>
|
872 | 877 | <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>
|
873 | 878 | <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 | 1436 | <pre><code class="python language-python hljs"><Path> = <Path>.parent <span class="hljs-comment"># Returns Path without the final component.</span>
|
1432 | 1437 | <str> = <Path>.name <span class="hljs-comment"># Returns final component as a string.</span>
|
1433 | 1438 | <str> = <Path>.suffix <span class="hljs-comment"># Returns name's last extension, e.g. '.py'.</span>
|
1434 |
| -<str> = <Path>.stem <span class="hljs-comment"># Returns name without last extension.</span> |
1435 |
| -<tup.> = <Path>.parts <span class="hljs-comment"># Returns all path's components as strings.</span> |
| 1439 | +<str> = <Path>.stem <span class="hljs-comment"># Returns name without the last extension.</span> |
| 1440 | +<tup.> = <Path>.parts <span class="hljs-comment"># Returns all components as strings.</span> |
1436 | 1441 | </code></pre>
|
1437 | 1442 | <pre><code class="python language-python hljs"><iter> = <Path>.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span>
|
1438 | 1443 | <iter> = <Path>.glob(<span class="hljs-string">'<pattern>'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span>
|
|
1522 | 1527 | </code></pre></div>
|
1523 | 1528 |
|
1524 | 1529 | <ul>
|
1525 |
| -<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> |
| 1530 | +<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> |
1526 | 1531 | <li><strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> library.</strong></li>
|
1527 | 1532 | <li><strong>For XML and binary Excel files (xlsx, xlsm and xlsb) use <a href="#dataframeplotencodedecode">Pandas</a> library.</strong></li>
|
1528 |
| -<li><strong>Reader accepts any iterator of strings, not just files.</strong></li> |
| 1533 | +<li><strong>Reader accepts any collection of strings, not just files.</strong></li> |
1529 | 1534 | </ul>
|
1530 | 1535 | <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>
|
1531 | 1536 | <writer>.writerow(<collection>) <span class="hljs-comment"># Encodes objects using `str(<el>)`.</span>
|
@@ -2940,7 +2945,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2940 | 2945 |
|
2941 | 2946 |
|
2942 | 2947 | <footer>
|
2943 |
| - <aside>May 9, 2025</aside> |
| 2948 | + <aside>May 12, 2025</aside> |
2944 | 2949 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2945 | 2950 | </footer>
|
2946 | 2951 |
|
|
0 commit comments