Skip to content

Commit 87dbc20

Browse files
committed
Class, Duck types, Paths, CSV, Deque
1 parent 566f27e commit 87dbc20

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ class MyClass:
971971
>>> obj.a, str(obj), repr(obj)
972972
(1, '1', 'MyClass(1)')
973973
```
974-
* **Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand, for example, `'print(a)'` calls `'a.__str__()'` and `'a + b'` calls `'a.__add__(b)'`.**
974+
* **Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand,<br>for example, `'print(a)'` calls `'a.__str__()'` and `'a + b'` calls `'a.__add__(b)'`.**
975975
* **Methods decorated with `'@staticmethod'` receive neither 'self' nor 'cls' argument.**
976976
* **Return value of str() special method should be readable and of repr() unambiguous. If only repr() is defined, it will also be used for str().**
977977

@@ -1042,7 +1042,7 @@ class <class_name>:
10421042
```
10431043
* **Objects can be made [sortable](#sortable) with `'order=True'` and immutable with `'frozen=True'`.**
10441044
* **For object to be [hashable](#hashable), all attributes must be hashable and 'frozen' must be True.**
1045-
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances. Its 'default_factory' argument can be any [callable](#callable).**
1045+
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances. Its 'default_factory' argument accepts any callable object.**
10461046
* **For attributes of arbitrary type use `'typing.Any'`.**
10471047

10481048
```python
@@ -1094,7 +1094,7 @@ Duck Types
10941094

10951095
### Comparable
10961096
* **If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`. That means all user-defined objects compare not equal by default (because id() returns object's memory address that is guaranteed to be unique).**
1097-
* **Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. False is returned if both return NotImplemented.**
1097+
* **Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. Result is False if both return NotImplemented.**
10981098
* **Ne() automatically works on any object that has eq() defined.**
10991099

11001100
```python
@@ -1172,7 +1172,7 @@ class Counter:
11721172
```
11731173

11741174
#### Python has many different iterator objects:
1175-
* **Sequence iterators returned by the [iter()](#iterator) function, such as list\_iterator and set\_iterator.**
1175+
* **Sequence iterators returned by the [iter()](#iterator) function, such as list\_iterator, etc.**
11761176
* **Objects returned by the [itertools](#itertools) module, such as count, repeat and cycle.**
11771177
* **Generators returned by the [generator functions](#generator) and [generator expressions](#comprehensions).**
11781178
* **File objects returned by the [open()](#open) function, etc.**
@@ -1407,7 +1407,7 @@ except (<exception>, [...]) as <name>: ...
14071407
```
14081408
* **Also catches subclasses, e.g. `'IndexError'` is caught by `'except LookupError:'`.**
14091409
* **Use `'traceback.print_exc()'` to print the full error message to standard error stream.**
1410-
* **Use `'print(<name>)'` to print just the cause of the exception (i.e. its arguments).**
1410+
* **Use `'print(<name>)'` to print just the cause of the exception (that is, its arguments).**
14111411
* **Use `'logging.exception(<str>)'` to log the passed message, followed by the full error message of the caught exception. For details about setting up the logger see [Logging](#logging).**
14121412
* **Use `'sys.exc_info()'` to get exception type, object, and traceback of caught exception.**
14131413

@@ -1549,7 +1549,7 @@ args = p.parse_args() # Exits on par
15491549

15501550
* **Use `'help=<str>'` to set argument description that will be displayed in help message.**
15511551
* **Use `'default=<obj>'` to override None as option's or optional argument's default value.**
1552-
* **Use `'type=FileType(<mode>)'` for files. Accepts 'encoding', but 'newline' is None.**
1552+
* **Use `'type=FileType(<mode>)'` for files. It accepts 'encoding', but 'newline' is None.**
15531553

15541554

15551555
Open
@@ -1657,7 +1657,7 @@ from pathlib import Path
16571657

16581658
```python
16591659
<iter> = os.scandir(path='.') # Returns DirEntry objects located at the path.
1660-
<str> = <DirEntry>.path # Returns object's path (relative by default).
1660+
<str> = <DirEntry>.path # Is absolute if 'path' argument was absolute.
16611661
<str> = <DirEntry>.name # Returns path's final component as a string.
16621662
<file> = open(<DirEntry>) # Opens the file and returns its file object.
16631663
```
@@ -1836,7 +1836,7 @@ import csv
18361836
* **`'lineterminator'` - How writer terminates rows. Reader looks for '\n', '\r' and '\r\n'.**
18371837
* **`'quotechar'` - Character for quoting fields containing delimiters, quotechars, '\n' or '\r'.**
18381838
* **`'escapechar'` - Character for escaping quotechars (not needed if doublequote is True).**
1839-
* **`'doublequote'` - Whether quotechars inside fields are (or get) doubled or escaped.**
1839+
* **`'doublequote'` - Whether quotechars inside fields are/get doubled instead of escaped.**
18401840
* **`'quoting'` - 0: As necessary, 1: All, 2: All but numbers which are read as floats, 3: None.**
18411841
* **`'skipinitialspace'` - Is space character at the start of the field stripped by the reader.**
18421842

@@ -2028,7 +2028,7 @@ b'\x00\x01\x00\x02\x00\x00\x00\x03'
20282028

20292029
Array
20302030
-----
2031-
**List that can only hold numbers of a predefined C type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.**
2031+
**List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.**
20322032

20332033
```python
20342034
from array import array
@@ -2084,7 +2084,7 @@ from collections import deque
20842084
```python
20852085
<deque> = deque(<collection>) # Use `maxlen=<int>` to set size limit.
20862086
<deque>.appendleft(<el>) # Opposite element is dropped if full.
2087-
<deque>.extendleft(<collection>) # Appends elements in reversed order.
2087+
<deque>.extendleft(<collection>) # Prepends reversed coll. to the deque.
20882088
<deque>.rotate(n=1) # Last element becomes the first one.
20892089
<el> = <deque>.popleft() # Raises IndexError if deque is empty.
20902090
```

index.html

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

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

@@ -827,7 +827,7 @@
827827
(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>, <span class="hljs-string">'MyClass(1)'</span>)
828828
</code></pre>
829829
<ul>
830-
<li><strong>Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand, for example, <code class="python hljs"><span class="hljs-string">'print(a)'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__str__()'</span></code> and <code class="python hljs"><span class="hljs-string">'a + b'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__add__(b)'</span></code>.</strong></li>
830+
<li><strong>Methods whose names start and end with two underscores are called special methods. They are executed when object is passed to a built-in function or used as an operand,<br>for example, <code class="python hljs"><span class="hljs-string">'print(a)'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__str__()'</span></code> and <code class="python hljs"><span class="hljs-string">'a + b'</span></code> calls <code class="python hljs"><span class="hljs-string">'a.__add__(b)'</span></code>.</strong></li>
831831
<li><strong>Methods decorated with <code class="python hljs"><span class="hljs-string">'@staticmethod'</span></code> receive neither 'self' nor 'cls' argument.</strong></li>
832832
<li><strong>Return value of str() special method should be readable and of repr() unambiguous. If only repr() is defined, it will also be used for str().</strong></li>
833833
</ul>
@@ -891,7 +891,7 @@
891891
<ul>
892892
<li><strong>Objects can be made <a href="#sortable">sortable</a> with <code class="python hljs"><span class="hljs-string">'order=True'</span></code> and immutable with <code class="python hljs"><span class="hljs-string">'frozen=True'</span></code>.</strong></li>
893893
<li><strong>For object to be <a href="#hashable">hashable</a>, all attributes must be hashable and 'frozen' must be True.</strong></li>
894-
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument can be any <a href="#callable">callable</a>.</strong></li>
894+
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: list = []'</span></code> would make a list that is shared among all instances. Its 'default_factory' argument accepts any callable object.</strong></li>
895895
<li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
896896
</ul>
897897
<pre><code class="python language-python hljs">P = make_dataclass(<span class="hljs-string">'P'</span>, [<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])
@@ -927,7 +927,7 @@
927927

928928
<div><h2 id="ducktypes"><a href="#ducktypes" name="ducktypes">#</a>Duck Types</h2><p><strong>A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.</strong></p><div><h3 id="comparable">Comparable</h3><ul>
929929
<li><strong>If eq() method is not overridden, it returns <code class="python hljs"><span class="hljs-string">'id(self) == id(other)'</span></code>, which is the same as <code class="python hljs"><span class="hljs-string">'self is other'</span></code>. That means all user-defined objects compare not equal by default (because id() returns object's memory address that is guaranteed to be unique).</strong></li>
930-
<li><strong>Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. False is returned if both return NotImplemented.</strong></li>
930+
<li><strong>Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted. Result is False if both return NotImplemented.</strong></li>
931931
<li><strong>Ne() automatically works on any object that has eq() defined.</strong></li>
932932
</ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyComparable</span>:</span>
933933
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
@@ -1002,7 +1002,7 @@
10021002
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
10031003
</code></pre>
10041004
<div><h4 id="pythonhasmanydifferentiteratorobjects">Python has many different iterator objects:</h4><ul>
1005-
<li><strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong></li>
1005+
<li><strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator, etc.</strong></li>
10061006
<li><strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong></li>
10071007
<li><strong>Generators returned by the <a href="#generator">generator functions</a> and <a href="#comprehensions">generator expressions</a>.</strong></li>
10081008
<li><strong>File objects returned by the <a href="#open">open()</a> function, etc.</strong></li>
@@ -1212,7 +1212,7 @@
12121212
<ul>
12131213
<li><strong>Also catches subclasses, e.g. <code class="python hljs"><span class="hljs-string">'IndexError'</span></code> is caught by <code class="python hljs"><span class="hljs-string">'except LookupError:'</span></code>.</strong></li>
12141214
<li><strong>Use <code class="python hljs"><span class="hljs-string">'traceback.print_exc()'</span></code> to print the full error message to standard error stream.</strong></li>
1215-
<li><strong>Use <code class="python hljs"><span class="hljs-string">'print(&lt;name&gt;)'</span></code> to print just the cause of the exception (i.e. its arguments).</strong></li>
1215+
<li><strong>Use <code class="python hljs"><span class="hljs-string">'print(&lt;name&gt;)'</span></code> to print just the cause of the exception (that is, its arguments).</strong></li>
12161216
<li><strong>Use <code class="python hljs"><span class="hljs-string">'logging.exception(&lt;str&gt;)'</span></code> to log the passed message, followed by the full error message of the caught exception. For details about setting up the logger see <a href="#logging">Logging</a>.</strong></li>
12171217
<li><strong>Use <code class="python hljs"><span class="hljs-string">'sys.exc_info()'</span></code> to get exception type, object, and traceback of caught exception.</strong></li>
12181218
</ul>
@@ -1327,7 +1327,7 @@
13271327
<ul>
13281328
<li><strong>Use <code class="python hljs"><span class="hljs-string">'help=&lt;str&gt;'</span></code> to set argument description that will be displayed in help message.</strong></li>
13291329
<li><strong>Use <code class="python hljs"><span class="hljs-string">'default=&lt;obj&gt;'</span></code> to override None as option's or optional argument's default value.</strong></li>
1330-
<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(&lt;mode&gt;)'</span></code> for files. Accepts 'encoding', but 'newline' is None.</strong></li>
1330+
<li><strong>Use <code class="python hljs"><span class="hljs-string">'type=FileType(&lt;mode&gt;)'</span></code> for files. It accepts 'encoding', but 'newline' is None.</strong></li>
13311331
</ul>
13321332
<div><h2 id="open"><a href="#open" name="open">#</a>Open</h2><p><strong>Opens a file and returns the corresponding file object.</strong></p><pre><code class="python language-python hljs">&lt;file&gt; = open(&lt;path&gt;, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>)
13331333
</code></pre></div>
@@ -1408,7 +1408,7 @@
14081408
&lt;num&gt; = &lt;stat&gt;.st_mtime/st_size/… <span class="hljs-comment"># Returns modification time, size in bytes, etc.</span>
14091409
</code></pre>
14101410
<div><h3 id="direntry">DirEntry</h3><p><strong>Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir, and on Windows also stat information, thus significantly increasing the performance of code that requires it.</strong></p><pre><code class="python language-python hljs">&lt;iter&gt; = os.scandir(path=<span class="hljs-string">'.'</span>) <span class="hljs-comment"># Returns DirEntry objects located at the path.</span>
1411-
&lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Returns object's path (relative by default).</span>
1411+
&lt;str&gt; = &lt;DirEntry&gt;.path <span class="hljs-comment"># Is absolute if 'path' argument was absolute.</span>
14121412
&lt;str&gt; = &lt;DirEntry&gt;.name <span class="hljs-comment"># Returns path's final component as a string.</span>
14131413
&lt;file&gt; = open(&lt;DirEntry&gt;) <span class="hljs-comment"># Opens the file and returns its file object.</span>
14141414
</code></pre></div>
@@ -1539,7 +1539,7 @@
15391539
<li><strong><code class="python hljs"><span class="hljs-string">'lineterminator'</span></code> - How writer terminates rows. Reader looks for '\n', '\r' and '\r\n'.</strong></li>
15401540
<li><strong><code class="python hljs"><span class="hljs-string">'quotechar'</span></code> - Character for quoting fields containing delimiters, quotechars, '\n' or '\r'.</strong></li>
15411541
<li><strong><code class="python hljs"><span class="hljs-string">'escapechar'</span></code> - Character for escaping quotechars (not needed if doublequote is True).</strong></li>
1542-
<li><strong><code class="python hljs"><span class="hljs-string">'doublequote'</span></code> - Whether quotechars inside fields are (or get) doubled or escaped.</strong></li>
1542+
<li><strong><code class="python hljs"><span class="hljs-string">'doublequote'</span></code> - Whether quotechars inside fields are/get doubled instead of escaped.</strong></li>
15431543
<li><strong><code class="python hljs"><span class="hljs-string">'quoting'</span></code> - 0: As necessary, 1: All, 2: All but numbers which are read as floats, 3: None.</strong></li>
15441544
<li><strong><code class="python hljs"><span class="hljs-string">'skipinitialspace'</span></code> - Is space character at the start of the field stripped by the reader.</strong></li>
15451545
</ul><div><h3 id="dialects">Dialects</h3><pre><code class="text language-text">┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓
@@ -1692,7 +1692,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
16921692

16931693

16941694

1695-
<div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined C type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array
1695+
<div><h2 id="array"><a href="#array" name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> array <span class="hljs-keyword">import</span> array
16961696
</code></pre></div>
16971697

16981698

@@ -1727,7 +1727,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17271727

17281728
<pre><code class="python language-python hljs">&lt;deque&gt; = deque(&lt;collection&gt;) <span class="hljs-comment"># Use `maxlen=&lt;int&gt;` to set size limit.</span>
17291729
&lt;deque&gt;.appendleft(&lt;el&gt;) <span class="hljs-comment"># Opposite element is dropped if full.</span>
1730-
&lt;deque&gt;.extendleft(&lt;collection&gt;) <span class="hljs-comment"># Appends elements in reversed order.</span>
1730+
&lt;deque&gt;.extendleft(&lt;collection&gt;) <span class="hljs-comment"># Prepends reversed coll. to the deque.</span>
17311731
&lt;deque&gt;.rotate(n=<span class="hljs-number">1</span>) <span class="hljs-comment"># Last element becomes the first one.</span>
17321732
&lt;el&gt; = &lt;deque&gt;.popleft() <span class="hljs-comment"># Raises IndexError if deque is empty.</span>
17331733
</code></pre>
@@ -2934,7 +2934,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29342934

29352935

29362936
<footer>
2937-
<aside>July 29, 2025</aside>
2937+
<aside>July 30, 2025</aside>
29382938
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29392939
</footer>
29402940

0 commit comments

Comments
 (0)