|
226 | 226 |
|
227 | 227 | <body>
|
228 | 228 | <header>
|
229 |
| - <aside>August 11, 2021</aside> |
| 229 | + <aside>October 1, 2021</aside> |
230 | 230 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
231 | 231 | </header>
|
232 | 232 |
|
|
786 | 786 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(*args, y, **kwargs)</span>:</span> <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3)</span>
|
787 | 787 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">f</span><span class="hljs-params">(x, *args, z, **kwargs)</span>:</span> <span class="hljs-comment"># f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)</span>
|
788 | 788 | </code></pre>
|
789 |
| -<div><h3 id="otheruses">Other Uses</h3><pre><code class="python language-python hljs"><list> = [*<collection> [, ...]] |
790 |
| -<set> = {*<collection> [, ...]} |
791 |
| -<tup.> = (*<collection>, [...]) |
792 |
| -<dict> = {**<dict> [, ...]} |
| 789 | +<div><h3 id="otheruses">Other Uses</h3><pre><code class="python language-python hljs"><list> = [*<collection> [, ...]] |
| 790 | +<set> = {*<collection> [, ...]} |
| 791 | +<tuple> = (*<collection>, [...]) |
| 792 | +<dict> = {**<dict> [, ...]} |
793 | 793 | </code></pre></div>
|
794 | 794 |
|
795 | 795 | <pre><code class="python language-python hljs">head, *body, tail = <collection>
|
|
816 | 816 | <ul>
|
817 | 817 | <li><strong>Reduce must be imported from functools module.</strong></li>
|
818 | 818 | </ul>
|
819 |
| -<div><h3 id="anyall">Any, All</h3><pre><code class="python language-python hljs"><bool> = any(<collection>) <span class="hljs-comment"># False if empty.</span> |
820 |
| -<bool> = all(el[<span class="hljs-number">1</span>] <span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> <collection>) <span class="hljs-comment"># True if empty.</span> |
| 819 | +<div><h3 id="anyall">Any, All</h3><pre><code class="python language-python hljs"><bool> = any(<collection>) <span class="hljs-comment"># Is `bool(el)` True for any element.</span> |
| 820 | +<bool> = all(<collection>) <span class="hljs-comment"># Is True for all elements or empty.</span> |
821 | 821 | </code></pre></div>
|
822 | 822 |
|
823 | 823 | <div><h3 id="conditionalexpression">Conditional Expression</h3><pre><code class="python language-python hljs"><obj> = <exp_if_true> <span class="hljs-keyword">if</span> <condition> <span class="hljs-keyword">else</span> <exp_if_false>
|
|
826 | 826 | <pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>[a <span class="hljs-keyword">if</span> a <span class="hljs-keyword">else</span> <span class="hljs-string">'zero'</span> <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)]
|
827 | 827 | [<span class="hljs-string">'zero'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
|
828 | 828 | </code></pre>
|
829 |
| -<div><h3 id="namedtupleenumdataclass">Namedtuple, Enum, Dataclass</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple |
830 |
| -Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>) |
831 |
| -point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>) |
| 829 | +<div><h3 id="namedtupleenumdataclass">Named Tuple, Enum, Dataclass</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> namedtuple |
| 830 | +Point = namedtuple(<span class="hljs-string">'Point'</span>, <span class="hljs-string">'x y'</span>) |
| 831 | +point = Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>) |
832 | 832 | </code></pre></div>
|
833 | 833 |
|
834 | 834 | <pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum
|
835 | 835 | Direction = Enum(<span class="hljs-string">'Direction'</span>, <span class="hljs-string">'n e s w'</span>)
|
836 | 836 | direction = Direction.n
|
837 | 837 | </code></pre>
|
838 | 838 | <pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> make_dataclass
|
839 |
| -Creature = make_dataclass(<span class="hljs-string">'Creature'</span>, [<span class="hljs-string">'loc'</span>, <span class="hljs-string">'dir'</span>]) |
840 |
| -creature = Creature(Point(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), Direction.n) |
| 839 | +Creature = make_dataclass(<span class="hljs-string">'Creature'</span>, [<span class="hljs-string">'loc'</span>, <span class="hljs-string">'dir'</span>]) |
| 840 | +creature = Creature(point, direction) |
841 | 841 | </code></pre>
|
842 | 842 | <div><h2 id="closure"><a href="#closure" name="closure">#</a>Closure</h2><p><strong>We have a closure in Python when:</strong></p><ul>
|
843 | 843 | <li><strong>A nested function references a value of its enclosing function and then</strong></li>
|
|
3005 | 3005 |
|
3006 | 3006 |
|
3007 | 3007 | <footer>
|
3008 |
| - <aside>August 11, 2021</aside> |
| 3008 | + <aside>October 1, 2021</aside> |
3009 | 3009 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
3010 | 3010 | </footer>
|
3011 | 3011 |
|
|
0 commit comments