Skip to content

Commit 036b94e

Browse files
committed
Inheritence to Subclass in Class, Paths, CSV
1 parent 0781789 commit 036b94e

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -995,29 +995,31 @@ Z = make_dataclass('Z', ['a']); print/str/repr(Z(<obj>))
995995
>>> <obj>
996996
```
997997

998-
### Inheritance
998+
### Subclass
999+
* **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.**
1000+
* **Subclass can then add its own methods and attributes or override inherited ones by reusing their names.**
1001+
9991002
```python
10001003
class Person:
10011004
def __init__(self, name):
10021005
self.name = name
1006+
def __repr__(self):
1007+
return f'Person({self.name!r})'
1008+
def __lt__(self, other):
1009+
return self.name < other.name
10031010

10041011
class Employee(Person):
10051012
def __init__(self, name, staff_num):
10061013
super().__init__(name)
10071014
self.staff_num = staff_num
1015+
def __repr__(self):
1016+
return f'Employee({self.name!r}, {self.staff_num})'
10081017
```
10091018

1010-
#### Multiple inheritance:
1011-
```python
1012-
class A: pass
1013-
class B: pass
1014-
class C(A, B): pass
1015-
```
1016-
1017-
**MRO determines the order in which parent classes are traversed when searching for a method or an attribute:**
10181019
```python
1019-
>>> C.mro()
1020-
[<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>]
1020+
>>> people = {Person('Ann'), Employee('Bob', 0)}
1021+
>>> sorted(people)
1022+
[Person('Ann'), Employee('Bob', 0)]
10211023
```
10221024

10231025
### Type Annotations
@@ -1685,8 +1687,8 @@ from pathlib import Path
16851687
<Path> = <Path>.parent # Returns Path without the final component.
16861688
<str> = <Path>.name # Returns final component as a string.
16871689
<str> = <Path>.suffix # Returns name's last extension, e.g. '.py'.
1688-
<str> = <Path>.stem # Returns name without last extension.
1689-
<tup.> = <Path>.parts # Returns all path's components as strings.
1690+
<str> = <Path>.stem # Returns name without the last extension.
1691+
<tup.> = <Path>.parts # Returns all components as strings.
16901692
```
16911693

16921694
```python
@@ -1820,10 +1822,10 @@ import csv
18201822
<list> = next(<reader>) # Returns next row as a list of strings.
18211823
<list> = list(<reader>) # Returns a list of remaining rows.
18221824
```
1823-
* **File must be opened with a `'newline=""'` argument, or newlines embedded inside quoted fields will not be interpreted correctly!**
1825+
* **File must be opened with a `'newline=""'` argument, or all '\r\n' sequences inside quoted fields will get converted to '\n'!**
18241826
* **To print the spreadsheet to the console use [Tabulate](#table) library.**
18251827
* **For XML and binary Excel files (xlsx, xlsm and xlsb) use [Pandas](#dataframe-plot-encode-decode) library.**
1826-
* **Reader accepts any iterator of strings, not just files.**
1828+
* **Reader accepts any collection of strings, not just files.**
18271829

18281830
### Write
18291831
```python

index.html

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<body>
5858
<header>
59-
<aside>May 9, 2025</aside>
59+
<aside>May 12, 2025</aside>
6060
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6161
</header>
6262

@@ -849,25 +849,30 @@
849849
<span class="hljs-meta">&gt;&gt;&gt; </span>&lt;obj&gt;
850850
</code></pre></div>
851851

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>
853856
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span>
854857
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 &lt; other.name
855862

856863
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Employee</span><span class="hljs-params">(Person)</span>:</span>
857864
<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>
858865
super().__init__(name)
859866
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>
860869
</code></pre></div>
861870

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>
866871

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">&gt;&gt;&gt; </span>C.mro()
869-
[&lt;<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">C</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">A</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">B</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">object</span>'&gt;]
870-
</span></code></pre>
872+
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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>
871876
<div><h3 id="typeannotations">Type Annotations</h3><ul>
872877
<li><strong>They add type hints to variables, arguments and functions (<code class="python hljs"><span class="hljs-string">'def f() -&gt; &lt;type&gt;:'</span></code>).</strong></li>
873878
<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 @@
14311436
<pre><code class="python language-python hljs">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns Path without the final component.</span>
14321437
&lt;str&gt; = &lt;Path&gt;.name <span class="hljs-comment"># Returns final component as a string.</span>
14331438
&lt;str&gt; = &lt;Path&gt;.suffix <span class="hljs-comment"># Returns name's last extension, e.g. '.py'.</span>
1434-
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns name without last extension.</span>
1435-
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all path's components as strings.</span>
1439+
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns name without the last extension.</span>
1440+
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all components as strings.</span>
14361441
</code></pre>
14371442
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Path&gt;.iterdir() <span class="hljs-comment"># Returns directory contents as Path objects.</span>
14381443
&lt;iter&gt; = &lt;Path&gt;.glob(<span class="hljs-string">'&lt;pattern&gt;'</span>) <span class="hljs-comment"># Returns Paths matching the wildcard pattern.</span>
@@ -1522,10 +1527,10 @@
15221527
</code></pre></div>
15231528

15241529
<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>
15261531
<li><strong>To print the spreadsheet to the console use <a href="#table">Tabulate</a> library.</strong></li>
15271532
<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>
15291534
</ul>
15301535
<div><h3 id="write">Write</h3><pre><code class="python language-python hljs">&lt;writer&gt; = csv.writer(&lt;file&gt;) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span>
15311536
&lt;writer&gt;.writerow(&lt;collection&gt;) <span class="hljs-comment"># Encodes objects using `str(&lt;el&gt;)`.</span>
@@ -2940,7 +2945,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29402945

29412946

29422947
<footer>
2943-
<aside>May 9, 2025</aside>
2948+
<aside>May 12, 2025</aside>
29442949
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29452950
</footer>
29462951

pdf/index_for_pdf.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ <h3 id="s">S</h3>
131131
<strong>strings, <a href="#abstractbaseclasses">4</a>-<a href="#comparisonofpresentationtypes">7</a>, <a href="#class">14</a></strong><br>
132132
<strong>struct module, <a href="#struct">28</a>-<a href="#integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">29</a></strong><br>
133133
<strong>subprocess module, <a href="#sends11tothebasiccalculatorandcapturesitsoutput">25</a></strong><br>
134-
<strong>super function, <a href="#inheritance">14</a></strong><br>
134+
<strong>super function, <a href="#subclass">14</a></strong><br>
135135
<strong>sys module, <a href="#cache">13</a>, <a href="#exit">21</a>-<a href="#commandlinearguments">22</a></strong> </p>
136136
<h3 id="t">T</h3>
137137
<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>

0 commit comments

Comments
 (0)