Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd53edb

Browse files
committed
Type annotations
1 parent d4c8e00 commit bd53edb

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def f(<nondefault_args>): # def f(x, y):
675675
def f(<default_args>): # def f(x=0, y=0):
676676
def f(<nondefault_args>, <default_args>): # def f(x, y=0):
677677
```
678-
* **A function has it's default values evaluated when it's first encountered in the scope.**
678+
* **A function has its default values evaluated when it's first encountered in the scope.**
679679
* **Any changes to mutable objects will persist between invocations.**
680680

681681

@@ -1043,7 +1043,7 @@ class <class_name>:
10431043
<attr_name_3>: list/dict/set = field(default_factory=list/dict/set)
10441044
```
10451045
* **Objects can be made [sortable](#sortable) with `'order=True'` and immutable with `'frozen=True'`.**
1046-
* **For object to be hashable, all attributes must be hashable and frozen must be True.**
1046+
* **For object to be [hashable](#hashable), all attributes must be hashable and frozen must be True.**
10471047
* **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).**
10481048
* **For attributes of arbitrary type use `'typing.Any'`.**
10491049

@@ -1055,6 +1055,13 @@ from dataclasses import make_dataclass
10551055
<tuple> = ('<attr_name>', <type> [, <default_value>])
10561056
```
10571057

1058+
#### Rest of type annotations (CPython interpreter ignores them all):
1059+
```python
1060+
def func(<arg_name>: <type> [= <obj>]) -> <type>:
1061+
<var_name>: typing.List/Set/Iterable/Sequence/Optional[<type>]
1062+
<var_name>: typing.Dict/Tuple/Union[<type>, ...]
1063+
```
1064+
10581065
### Slots
10591066
**Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.**
10601067

@@ -1068,7 +1075,6 @@ class MyClassWithSlots:
10681075
### Copy
10691076
```python
10701077
from copy import copy, deepcopy
1071-
10721078
<object> = copy(<object>)
10731079
<object> = deepcopy(<object>)
10741080
```

index.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>December 30, 2021</aside>
57+
<aside>January 1, 2022</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -598,7 +598,7 @@
598598
</code></pre></div>
599599

600600
<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>
602602
<li><strong>Any changes to mutable objects will persist between invocations.</strong></li>
603603
</ul>
604604
<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 @@
886886

887887
<ul>
888888
<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>
890890
<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>
891891
<li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
892892
</ul>
@@ -895,6 +895,11 @@
895895
&lt;class&gt; = make_dataclass(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;coll_of_tuples&gt;)
896896
&lt;tuple&gt; = (<span class="hljs-string">'&lt;attr_name&gt;'</span>, &lt;type&gt; [, &lt;default_value&gt;])</code></pre></div>
897897

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">(&lt;arg_name&gt;: &lt;type&gt; [= &lt;obj&gt;])</span> -&gt; &lt;type&gt;:</span>
899+
&lt;var_name&gt;: typing.List/Set/Iterable/Sequence/Optional[&lt;type&gt;]
900+
&lt;var_name&gt;: typing.Dict/Tuple/Union[&lt;type&gt;, ...]
901+
</code></pre></div>
902+
898903
<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>
899904
__slots__ = [<span class="hljs-string">'a'</span>]
900905
<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 @@
903908

904909

905910
<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-
907911
&lt;object&gt; = copy(&lt;object&gt;)
908912
&lt;object&gt; = deepcopy(&lt;object&gt;)
909913
</code></pre></div>
@@ -2876,7 +2880,7 @@
28762880

28772881

28782882
<footer>
2879-
<aside>December 30, 2021</aside>
2883+
<aside>January 1, 2022</aside>
28802884
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
28812885
</footer>
28822886

pdf/index_for_pdf.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ <h3 id="s">S</h3>
132132
<strong>struct module, <a href="#struct">28</a>-<a href="#integertypesuseacapitalletterforunsignedtypeminimumandstandardsizesareinbrackets">29</a></strong><br>
133133
<strong>subprocess module, <a href="#sends11tothebasiccalculatorandcapturesitsoutput">25</a></strong><br>
134134
<strong>super function, <a href="#inheritance">14</a></strong><br>
135-
<strong>synthesizer, <a href="#synthesizer">41</a></strong><br>
136135
<strong>sys module, <a href="#lrucache">13</a>, <a href="#exit">21</a>-<a href="#commandlinearguments">22</a></strong> </p>
137136
<h3 id="t">T</h3>
138137
<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>
139138
<strong>template, <a href="#format">6</a>, <a href="#dynamicrequest">36</a></strong><br>
140139
<strong>threading module, <a href="#threading">30</a></strong><br>
141140
<strong>time module, <a href="#progressbar">34</a>, <a href="#stopwatch">36</a></strong><br>
142141
<strong>tuples, <a href="#tuple">3</a>, <a href="#abstractbaseclasses">4</a>, <a href="#otheruses">11</a></strong><br>
143-
<strong>type, <a href="#type">4</a>, <a href="#metaprogramming">31</a>-<a href="#metaclass">32</a></strong> </p>
142+
<strong>type, <a href="#type">4</a>, <a href="#metaprogramming">31</a>-<a href="#metaclass">32</a></strong><br>
143+
<strong>type annotations, <a href="#dataclass">15</a></strong> </p>
144144
<h3 id="w">W</h3>
145145
<p><strong>wave module, <a href="#audio">40</a>-<a href="#writefloatsamplestowavfile">41</a></strong><br>
146146
<strong>web, <a href="#web">36</a></strong> </p>

pdf/index_for_pdf_print.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ <h3 id="s">S</h3>
132132
<strong>struct module, 28-29</strong><br>
133133
<strong>subprocess module, 25</strong><br>
134134
<strong>super function, 14</strong><br>
135-
<strong>synthesizer, 41</strong><br>
136135
<strong>sys module, 13, 21-22</strong> </p>
137136
<h3 id="t">T</h3>
138137
<p><strong>table, 26, 27, 34, 37-38, 45-46</strong><br>
139138
<strong>template, 6, 36</strong><br>
140139
<strong>threading module, 30</strong><br>
141140
<strong>time module, 34, 36</strong><br>
142141
<strong>tuples, 3, 4, 11</strong><br>
143-
<strong>type, 4, 31-32</strong> </p>
142+
<strong>type, 4, 31-32</strong><br>
143+
<strong>type annotations, 15</strong> </p>
144144
<h3 id="w">W</h3>
145145
<p><strong>wave module, 40-41</strong><br>
146146
<strong>web, 36</strong> </p>

pdf/remove_links.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'<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>',
1313
'<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>',
1414
'<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>',
15+
'<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>',
1516
'<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>',
1617
'<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>',
1718
'<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>',

0 commit comments

Comments
 (0)