|
226 | 226 |
|
227 | 227 | <body>
|
228 | 228 | <header>
|
229 |
| - <aside>October 4, 2021</aside> |
| 229 | + <aside>October 7, 2021</aside> |
230 | 230 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
231 | 231 | </header>
|
232 | 232 |
|
|
546 | 546 | {<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span>
|
547 | 547 | </code></pre></div>
|
548 | 548 |
|
549 |
| -<div><h3 id="numbers-1">Numbers</h3><pre><code class="python language-python hljs">{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>} <span class="hljs-comment"># ' 123456'</span> |
550 |
| -{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span> |
551 |
| -{ <span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span> |
552 |
| -{ <span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span> |
553 |
| -{<span class="hljs-number">-123456</span>:=<span class="hljs-number">10</span>} <span class="hljs-comment"># '- 123456'</span> |
554 |
| -{ <span class="hljs-number">123456</span>: } <span class="hljs-comment"># ' 123456'</span> |
| 549 | +<div><h3 id="numbers-1">Numbers</h3><pre><code class="python language-python hljs">{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>} <span class="hljs-comment"># ' 123456'</span> |
| 550 | +{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>,} <span class="hljs-comment"># ' 123,456'</span> |
| 551 | +{<span class="hljs-number">123456</span>:<span class="hljs-number">10</span>_} <span class="hljs-comment"># ' 123_456'</span> |
| 552 | +{<span class="hljs-number">123456</span>:+<span class="hljs-number">10</span>} <span class="hljs-comment"># ' +123456'</span> |
| 553 | +{<span class="hljs-number">123456</span>:=+<span class="hljs-number">10</span>} <span class="hljs-comment"># '+ 123456'</span> |
| 554 | +{<span class="hljs-number">123456</span>: } <span class="hljs-comment"># ' 123456'</span> |
555 | 555 | {<span class="hljs-number">-123456</span>: } <span class="hljs-comment"># '-123456'</span>
|
556 | 556 | </code></pre></div>
|
557 | 557 |
|
|
1025 | 1025 |
|
1026 | 1026 |
|
1027 | 1027 | <ul>
|
| 1028 | +<li><strong>For arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li> |
1028 | 1029 | <li><strong>Objects can be made sortable 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>
|
1029 | 1030 | <li><strong>For object to be hashable, all attributes must be hashable and frozen must be True.</strong></li>
|
1030 | 1031 | <li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'<attr_name>: list = []'</span></code> would make a list that is shared among all instances.</strong></li>
|
|
1291 | 1292 |
|
1292 | 1293 | <div><h4 id="userdefinedfunctionscannotbevaluessotheymustbewrapped">User-defined functions cannot be values, so they must be wrapped:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> partial
|
1293 | 1294 | LogicOp = Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">and</span> r),
|
1294 |
| - <span class="hljs-string">'OR'</span> : partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)}) |
| 1295 | + <span class="hljs-string">'OR'</span>: partial(<span class="hljs-keyword">lambda</span> l, r: l <span class="hljs-keyword">or</span> r)}) |
1295 | 1296 | </code></pre></div>
|
1296 | 1297 |
|
1297 | 1298 | <ul>
|
1298 |
| -<li><strong>Another solution in this particular case is to use functions and_() and or_() from the module <a href="#operator">operator</a>.</strong></li> |
| 1299 | +<li><strong>Member names are in all caps because trying to access a member that is named after a reserved keyword raises SyntaxError.</strong></li> |
1299 | 1300 | </ul>
|
1300 | 1301 | <div><h2 id="exceptions"><a href="#exceptions" name="exceptions">#</a>Exceptions</h2><div><h3 id="basicexample">Basic Example</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">try</span>:
|
1301 | 1302 | <code>
|
|
1318 | 1319 |
|
1319 | 1320 | <ul>
|
1320 | 1321 | <li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'else'</span></code> block will only be executed if <code class="python hljs"><span class="hljs-string">'try'</span></code> block had no exceptions.</strong></li>
|
1321 |
| -<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed.</strong></li> |
| 1322 | +<li><strong>Code inside the <code class="python hljs"><span class="hljs-string">'finally'</span></code> block will always be executed (unless a signal is received).</strong></li> |
1322 | 1323 | </ul>
|
1323 | 1324 | <div><h3 id="catchingexceptions">Catching Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">except</span> <exception>:
|
1324 | 1325 | <span class="hljs-keyword">except</span> <exception> <span class="hljs-keyword">as</span> <name>:
|
|
1903 | 1904 | sorted_by_both = sorted(<collection>, key=op.itemgetter(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>))
|
1904 | 1905 | product_of_elems = functools.reduce(op.mul, <collection>)
|
1905 | 1906 | union_of_sets = functools.reduce(op.or_, <coll_of_sets>)
|
1906 |
| -LogicOp = enum.Enum(<span class="hljs-string">'LogicOp'</span>, {<span class="hljs-string">'AND'</span>: op.and_, <span class="hljs-string">'OR'</span>: op.or_}) |
1907 | 1907 | last_el = op.methodcaller(<span class="hljs-string">'pop'</span>)(<list>)
|
1908 | 1908 | </code></pre>
|
1909 | 1909 | <div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs"><list> = dir() <span class="hljs-comment"># Names of local variables (incl. functions).</span>
|
|
3007 | 3007 |
|
3008 | 3008 |
|
3009 | 3009 | <footer>
|
3010 |
| - <aside>October 4, 2021</aside> |
| 3010 | + <aside>October 7, 2021</aside> |
3011 | 3011 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
3012 | 3012 | </footer>
|
3013 | 3013 |
|
|
0 commit comments