|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>December 30, 2021</aside> |
| 57 | + <aside>January 1, 2022</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
598 | 598 | </code></pre></div>
|
599 | 599 |
|
600 | 600 | <ul>
|
601 |
| -<li><strong>A function has it's default values evaluated when it's first encountered in the scope.</strong></li> |
| 601 | +<li><strong>A function has its default values evaluated when it's first encountered in the scope.</strong></li> |
602 | 602 | <li><strong>Any changes to mutable objects will persist between invocations.</strong></li>
|
603 | 603 | </ul>
|
604 | 604 | <div><h2 id="splatoperator"><a href="#splatoperator" name="splatoperator">#</a>Splat Operator</h2><div><h3 id="insidefunctioncall-1">Inside Function Call</h3><p><strong>Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.</strong></p><pre><code class="python language-python hljs">args = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
|
|
886 | 886 |
|
887 | 887 | <ul>
|
888 | 888 | <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>
|
889 |
| -<li><strong>For object to be hashable, all attributes must be hashable and frozen must be True.</strong></li> |
| 889 | +<li><strong>For object to be <a href="#hashable">hashable</a>, all attributes must be hashable and frozen must be True.</strong></li> |
890 | 890 | <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. Its 'default_factory' argument can be any <a href="#callable">callable</a>.</strong></li>
|
891 | 891 | <li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
|
892 | 892 | </ul>
|
|
895 | 895 | <class> = make_dataclass(<span class="hljs-string">'<class_name>'</span>, <coll_of_tuples>)
|
896 | 896 | <tuple> = (<span class="hljs-string">'<attr_name>'</span>, <type> [, <default_value>])</code></pre></div>
|
897 | 897 |
|
| 898 | +<div><h4 id="restoftypeannotationscpythoninterpreterignoresthemall">Rest of type annotations (CPython interpreter ignores them all):</h4><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(<arg_name>: <type> [= <obj>])</span> -> <type>:</span> |
| 899 | +<var_name>: typing.List/Set/Iterable/Sequence/Optional[<type>] |
| 900 | +<var_name>: typing.Dict/Tuple/Union[<type>, ...] |
| 901 | +</code></pre></div> |
| 902 | + |
898 | 903 | <div><h3 id="slots">Slots</h3><p><strong>Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClassWithSlots</span>:</span>
|
899 | 904 | __slots__ = [<span class="hljs-string">'a'</span>]
|
900 | 905 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
|
|
903 | 908 |
|
904 | 909 |
|
905 | 910 | <div><h3 id="copy">Copy</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> copy <span class="hljs-keyword">import</span> copy, deepcopy
|
906 |
| - |
907 | 911 | <object> = copy(<object>)
|
908 | 912 | <object> = deepcopy(<object>)
|
909 | 913 | </code></pre></div>
|
|
2876 | 2880 |
|
2877 | 2881 |
|
2878 | 2882 | <footer>
|
2879 |
| - <aside>December 30, 2021</aside> |
| 2883 | + <aside>January 1, 2022</aside> |
2880 | 2884 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2881 | 2885 | </footer>
|
2882 | 2886 |
|
|
0 commit comments