|
1070 | 1070 | </code></pre> |
1071 | 1071 | <div><h3 id="collection">Collection</h3><ul> |
1072 | 1072 | <li><strong>Only required methods are iter() and len(). Len() should return the number of items.</strong></li> |
1073 | | -<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> |
| 1073 | +<li><strong>This cheatsheet actually means <code class="python hljs"><span class="hljs-string">'<iterable>'</span></code> when it uses the <code class="python hljs"><span class="hljs-string">'<collection>'</span></code>.</strong></li> |
1074 | 1074 | <li><strong>I chose not to use the name 'iterable' because it sounds scarier and more vague than 'collection'. The main drawback of this decision is that the 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> |
1075 | 1075 | </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> |
1076 | 1076 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span> |
@@ -1772,11 +1772,11 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
1772 | 1772 | </code></pre></div> |
1773 | 1773 |
|
1774 | 1774 | <ul> |
1775 | | -<li><strong>Sequence pattern can also be written as a tuple, i.e. <code class="python hljs"><span class="hljs-string">'(<pattern_1>, [...])'</span></code>.</strong></li> |
| 1775 | +<li><strong>Sequence pattern can also be written as a tuple, either with or without the brackets.</strong></li> |
1776 | 1776 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'*<name>'</span></code> and <code class="python hljs"><span class="hljs-string">'**<name>'</span></code> in sequence/mapping patterns to bind remaining items.</strong></li> |
1777 | 1777 | <li><strong>Sequence pattern must match all items of the collection, while mapping pattern does not.</strong></li> |
1778 | | -<li><strong>Patterns can be surrounded with brackets to override precedence (<code class="python hljs"><span class="hljs-string">'|'</span></code> > <code class="python hljs"><span class="hljs-string">'as'</span></code> > <code class="python hljs"><span class="hljs-string">','</span></code>).</strong></li> |
1779 | | -<li><strong>All names that are bound in the matching case, as well as variables initialized in its block, are visible after the match statement.</strong></li> |
| 1778 | +<li><strong>Patterns can be surrounded with brackets to override their precedence: <code class="python hljs"><span class="hljs-string">'|'</span></code> > <code class="python hljs"><span class="hljs-string">'as'</span></code> > <code class="python hljs"><span class="hljs-string">','</span></code>. For example, <code class="python hljs"><span class="hljs-string">'[1, 2]'</span></code> gets caught by the <code class="python hljs"><span class="hljs-string">'case 1 | 0, 2 as x if x == 2'</span></code> clause.</strong></li> |
| 1779 | +<li><strong>All names that are bound in the matching case, as well as variables initialized in its block, are visible after the match statement (only the function block delimits scope).</strong></li> |
1780 | 1780 | </ul> |
1781 | 1781 | <div><h3 id="example-2">Example</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path |
1782 | 1782 | <span class="hljs-meta">>>> </span><span class="hljs-keyword">match</span> Path(<span class="hljs-string">'/home/gto/python-cheatsheet/README.md'</span>): |
@@ -1897,7 +1897,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment |
1897 | 1897 | </code></pre> |
1898 | 1898 | <ul> |
1899 | 1899 | <li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As_completed() fails if next() is called too late, even if all threads are done.</strong></li> |
1900 | | -<li><strong>Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns exception object or None.</strong></li> |
| 1900 | +<li><strong>Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns an exception object or None.</strong></li> |
1901 | 1901 | <li><strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <a href="#pickle">pickable</a>, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via <code class="python hljs"><span class="hljs-string">'if __name__ == "__main__": ...'</span></code>.</strong></li> |
1902 | 1902 | </ul> |
1903 | 1903 | <div><h2 id="coroutines"><a href="#coroutines" name="coroutines">#</a>Coroutines</h2><ul> |
|
0 commit comments