Browse Source

Type annotations

pull/118/head
Jure Šorn 3 years ago
parent
commit
bd53edb6d0
5 changed files with 23 additions and 12 deletions
  1. 12
      README.md
  2. 14
      index.html
  3. 4
      pdf/index_for_pdf.html
  4. 4
      pdf/index_for_pdf_print.html
  5. 1
      pdf/remove_links.py

12
README.md

@ -675,7 +675,7 @@ def f(<nondefault_args>): # def f(x, y):
def f(<default_args>): # def f(x=0, y=0):
def f(<nondefault_args>, <default_args>): # def f(x, y=0):
```
* **A function has it's default values evaluated when it's first encountered in the scope.**
* **A function has its default values evaluated when it's first encountered in the scope.**
* **Any changes to mutable objects will persist between invocations.**
@ -1043,7 +1043,7 @@ class <class_name>:
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
```
* **Objects can be made [sortable](#sortable) with `'order=True'` and immutable with `'frozen=True'`.**
* **For object to be hashable, all attributes must be hashable and frozen must be True.**
* **For object to be [hashable](#hashable), all attributes must be hashable and frozen must be True.**
* **Function field() is needed because `'<attr_name>: list = []'` would make a list that is shared among all instances. Its 'default_factory' argument can be any [callable](#callable).**
* **For attributes of arbitrary type use `'typing.Any'`.**
@ -1055,6 +1055,13 @@ from dataclasses import make_dataclass
<tuple> = ('<attr_name>', <type> [, <default_value>])
```
#### Rest of type annotations (CPython interpreter ignores them all):
```python
def func(<arg_name>: <type> [= <obj>]) -> <type>:
<var_name>: typing.List/Set/Iterable/Sequence/Optional[<type>]
<var_name>: typing.Dict/Tuple/Union[<type>, ...]
```
### Slots
**Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.**
@ -1068,7 +1075,6 @@ class MyClassWithSlots:
### Copy
```python
from copy import copy, deepcopy
<object> = copy(<object>)
<object> = deepcopy(<object>)
```

14
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>December 30, 2021</aside>
<aside>January 1, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -598,7 +598,7 @@ to_exclusive = &lt;range&gt;.stop
</code></pre></div>
<ul>
<li><strong>A function has it's default values evaluated when it's first encountered in the scope.</strong></li>
<li><strong>A function has its default values evaluated when it's first encountered in the scope.</strong></li>
<li><strong>Any changes to mutable objects will persist between invocations.</strong></li>
</ul>
<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,7 +886,7 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<ul>
<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>
<li><strong>For object to be hashable, all attributes must be hashable and frozen must be True.</strong></li>
<li><strong>For object to be <a href="#hashable">hashable</a>, all attributes must be hashable and frozen must be True.</strong></li>
<li><strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">'&lt;attr_name&gt;: 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>
<li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
</ul>
@ -895,6 +895,11 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
&lt;class&gt; = make_dataclass(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;coll_of_tuples&gt;)
&lt;tuple&gt; = (<span class="hljs-string">'&lt;attr_name&gt;'</span>, &lt;type&gt; [, &lt;default_value&gt;])</code></pre></div>
<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">(&lt;arg_name&gt;: &lt;type&gt; [= &lt;obj&gt;])</span> -&gt; &lt;type&gt;:</span>
&lt;var_name&gt;: typing.List/Set/Iterable/Sequence/Optional[&lt;type&gt;]
&lt;var_name&gt;: typing.Dict/Tuple/Union[&lt;type&gt;, ...]
</code></pre></div>
<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>
__slots__ = [<span class="hljs-string">'a'</span>]
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
@ -903,7 +908,6 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<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
&lt;object&gt; = copy(&lt;object&gt;)
&lt;object&gt; = deepcopy(&lt;object&gt;)
</code></pre></div>
@ -2876,7 +2880,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>December 30, 2021</aside>
<aside>January 1, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

4
pdf/index_for_pdf.html

@ -132,7 +132,6 @@
<strong>struct module, <a href="#struct">28</a>-<a href="#integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">29</a></strong><br>
<strong>subprocess module, <a href="#sends11tothebasiccalculatorandcapturesitsoutput">25</a></strong><br>
<strong>super function, <a href="#inheritance">14</a></strong><br>
<strong>synthesizer, <a href="#synthesizer">41</a></strong><br>
<strong>sys module, <a href="#lrucache">13</a>, <a href="#exit">21</a>-<a href="#commandlinearguments">22</a></strong> </p>
<h3 id="t">T</h3>
<p><strong>table, <a href="#csv">26</a>, <a href="#example">27</a>, <a href="#table">34</a>, <a href="#numpy">37</a>-<a href="#indexing">38</a>, <a href="#dataframe">45</a>-<a href="#aggregatetransformmap-1">46</a></strong><br>
@ -140,7 +139,8 @@
<strong>threading module, <a href="#threading">30</a></strong><br>
<strong>time module, <a href="#progressbar">34</a>, <a href="#stopwatch">36</a></strong><br>
<strong>tuples, <a href="#tuple">3</a>, <a href="#abstractbaseclasses">4</a>, <a href="#otheruses">11</a></strong><br>
<strong>type, <a href="#type">4</a>, <a href="#metaprogramming">31</a>-<a href="#metaclass">32</a></strong> </p>
<strong>type, <a href="#type">4</a>, <a href="#metaprogramming">31</a>-<a href="#metaclass">32</a></strong><br>
<strong>type annotations, <a href="#dataclass">15</a></strong> </p>
<h3 id="w">W</h3>
<p><strong>wave module, <a href="#audio">40</a>-<a href="#writefloatsamplestowavfile">41</a></strong><br>
<strong>web, <a href="#web">36</a></strong> </p>

4
pdf/index_for_pdf_print.html

@ -132,7 +132,6 @@
<strong>struct module, 28-29</strong><br>
<strong>subprocess module, 25</strong><br>
<strong>super function, 14</strong><br>
<strong>synthesizer, 41</strong><br>
<strong>sys module, 13, 21-22</strong> </p>
<h3 id="t">T</h3>
<p><strong>table, 26, 27, 34, 37-38, 45-46</strong><br>
@ -140,7 +139,8 @@
<strong>threading module, 30</strong><br>
<strong>time module, 34, 36</strong><br>
<strong>tuples, 3, 4, 11</strong><br>
<strong>type, 4, 31-32</strong> </p>
<strong>type, 4, 31-32</strong><br>
<strong>type annotations, 15</strong> </p>
<h3 id="w">W</h3>
<p><strong>wave module, 40-41</strong><br>
<strong>web, 36</strong> </p>

1
pdf/remove_links.py

@ -12,6 +12,7 @@ MATCHES = {
'<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its <a href="#class">repr()</a> method.</strong>': '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its repr() method (p. 14).</strong>',
'<strong>It can be any <a href="#callable">callable</a>, but is usually implemented as a function that returns a <a href="#closure">closure</a>.</strong>': '<strong>It can be any callable (p. 17), but is usually implemented as a function that returns a closure (p. 12).</strong>',
'<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>': '<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>',
'<strong>For object to be <a href="#hashable">hashable</a>, all attributes must be hashable and frozen must be True.</strong>': '<strong>For object to be hashable, all attributes must be hashable and frozen must be True.</strong>',
'<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: 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>': '<strong>Function field() is needed because <code class="python hljs"><span class="hljs-string">\'&lt;attr_name&gt;: list = []\'</span></code> would make a list that is shared among all instances. Its \'default_factory\' argument can be any callable (p. 17).</strong>',
'<strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong>': '<strong>Sequence iterators returned by the iter() function, such as list_iterator and set_iterator (p.&nbsp;3).</strong>',
'<strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong>': '<strong>Objects returned by the itertools module, such as count, repeat and cycle (p. 3).</strong>',

Loading…
Cancel
Save