|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>March 1, 2022</aside> |
| 57 | + <aside>March 2, 2022</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
1057 | 1057 | <div><h3 id="collection">Collection</h3><ul>
|
1058 | 1058 | <li><strong>Only required methods are iter() and len().</strong></li>
|
1059 | 1059 | <li><strong>This cheatsheet actually means <code class="python hljs"><span class="hljs-string">'<iterable>'</span></code> when it uses <code class="python hljs"><span class="hljs-string">'<collection>'</span></code>.</strong></li>
|
1060 |
| -<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only iterable objects that are not collections.</strong></li> |
| 1060 | +<li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The only drawback of this decision is that a reader could think a certain function doesn't accept iterators when it does, since iterators are the only built-in objects that are iterable but are not collections.</strong></li> |
1061 | 1061 | </ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCollection</span>:</span>
|
1062 | 1062 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
|
1063 | 1063 | self.a = a
|
|
1319 | 1319 | <ul>
|
1320 | 1320 | <li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means that the default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> whenever possible.</strong></li>
|
1321 | 1321 | <li><strong><code class="python hljs"><span class="hljs-string">'newline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li>
|
1322 |
| -<li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on either '\n', '\r' or '\r\n'.</strong></li> |
| 1322 | +<li><strong><code class="python hljs"><span class="hljs-string">'newline=""'</span></code> means no conversions take place, but input is still broken into chunks by readline() and readlines() on '\n', '\r' and '\r\n'.</strong></li> |
1323 | 1323 | </ul>
|
1324 | 1324 | <div><h3 id="modes">Modes</h3><ul>
|
1325 | 1325 | <li><strong><code class="python hljs"><span class="hljs-string">'r'</span></code> - Read (default).</strong></li>
|
|
1353 | 1353 | </code></pre>
|
1354 | 1354 | <pre><code class="python language-python hljs"><file>.write(<str/bytes>) <span class="hljs-comment"># Writes a string or bytes object.</span>
|
1355 | 1355 | <file>.writelines(<collection>) <span class="hljs-comment"># Writes a coll. of strings or bytes objects.</span>
|
1356 |
| -<file>.flush() <span class="hljs-comment"># Flushes write buffer.</span> |
| 1356 | +<file>.flush() <span class="hljs-comment"># Flushes write buffer. Runs every 4096/8192 B.</span> |
1357 | 1357 | </code></pre>
|
1358 | 1358 | <ul>
|
1359 | 1359 | <li><strong>Methods do not add or strip trailing newlines, even writelines().</strong></li>
|
|
2884 | 2884 |
|
2885 | 2885 |
|
2886 | 2886 | <footer>
|
2887 |
| - <aside>March 1, 2022</aside> |
| 2887 | + <aside>March 2, 2022</aside> |
2888 | 2888 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2889 | 2889 | </footer>
|
2890 | 2890 |
|
|
0 commit comments