Browse Source

Inheritence to Subclass in Class, Paths, CSV

pull/102/merge
Jure Šorn 1 month ago
parent
commit
036b94e668
3 changed files with 38 additions and 31 deletions
  1. 32
      README.md
  2. 35
      index.html
  3. 2
      pdf/index_for_pdf.html

32
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

35
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">&gt;&gt;&gt; </span>&lt;obj&gt;
</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 &lt; 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">&gt;&gt;&gt; </span>C.mro()
[&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;]
</span></code></pre>
<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>)}
<span class="hljs-meta">&gt;&gt;&gt; </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() -&gt; &lt;type&gt;:'</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">&lt;Path&gt; = &lt;Path&gt;.parent <span class="hljs-comment"># Returns Path without the final component.</span>
&lt;str&gt; = &lt;Path&gt;.name <span class="hljs-comment"># Returns final component as a string.</span>
&lt;str&gt; = &lt;Path&gt;.suffix <span class="hljs-comment"># Returns name's last extension, e.g. '.py'.</span>
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns name without last extension.</span>
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all path's components as strings.</span>
&lt;str&gt; = &lt;Path&gt;.stem <span class="hljs-comment"># Returns name without the last extension.</span>
&lt;tup.&gt; = &lt;Path&gt;.parts <span class="hljs-comment"># Returns all components as strings.</span>
</code></pre>
<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>
&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 @@ 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">&lt;writer&gt; = csv.writer(&lt;file&gt;) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span>
&lt;writer&gt;.writerow(&lt;collection&gt;) <span class="hljs-comment"># Encodes objects using `str(&lt;el&gt;)`.</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>

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

Loading…
Cancel
Save