@ -840,7 +840,7 @@ import <package>.<module> # Imports a built-in or '<package>/<module>.py'.
Closure
-------
**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.**
**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.**
```python
def get_multiplier(a):
@ -2717,50 +2717,49 @@ import numpy as np
### Broadcasting
**A set of rules by which NumPy functions operate on arrays of different shapes.**
@ -701,7 +701,7 @@ player = Player(point, direction) <span class="hljs-comment">#
<li><strong>Directory of the file that is passed to python command serves as a root of local imports.</strong></li>
<li><strong>For relative imports use <codeclass="python hljs"><spanclass="hljs-string">'from .[…][<pkg/module>[.…]] import <obj>'</span></code>.</strong></li>
</ul>
<div><h2id="closure"><ahref="#closure"name="closure">#</a>Closure</h2><p><strong>We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">get_multiplier</span><spanclass="hljs-params">(a)</span>:</span>
<div><h2id="closure"><ahref="#closure"name="closure">#</a>Closure</h2><p><strong>We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">get_multiplier</span><spanclass="hljs-params">(a)</span>:</span>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'ix_([1, 2], [3, 4])'</span></code> returns <codeclass="python hljs"><spanclass="hljs-string">'[[1], [2]]'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'[[3, 4]]'</span></code>. Due to broadcasting rules, this is the same as using <codeclass="python hljs"><spanclass="hljs-string">'[[1, 1], [2, 2]]'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'[[3, 4], [3, 4]]'</span></code>.</strong></li>
<li><strong>Any value that is broadcastable to the indexed shape can be assigned to the selection.</strong></li>
</ul>
<div><h3id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><codeclass="python language-python hljs">left = [ <spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span> ]<spanclass="hljs-comment"># Shape: (3,)</span>
right = [[<spanclass="hljs-number">0.1</span>],[<spanclass="hljs-number">0.6</span>],[<spanclass="hljs-number">0.8</span>]]<spanclass="hljs-comment"># Shape: (3, 1)</span>
<div><h3id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><codeclass="python language-python hljs">left = np.array([ <spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span> ])<spanclass="hljs-comment"># `left.shape == (3,)`</span>
right = np.array([[<spanclass="hljs-number">0.1</span>],[<spanclass="hljs-number">0.6</span>],[<spanclass="hljs-number">0.8</span>]])<spanclass="hljs-comment"># `right.shape == (3, 1)`</span>
</code></pre></div>
<div><h4id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><codeclass="python language-python hljs">left = [[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>]]<spanclass="hljs-comment"># Shape: (1, 3) <- !</span>
right = [[<spanclass="hljs-number">0.1</span>],[<spanclass="hljs-number">0.6</span>],[<spanclass="hljs-number">0.8</span>]]<spanclass="hljs-comment"># Shape: (3, 1)</span>
<div><h4id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><codeclass="python language-python hljs">left = np.array([[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>]])<spanclass="hljs-comment"># `left.shape == (1, 3)`</span>
right = np.array([[<spanclass="hljs-number">0.1</span>],[<spanclass="hljs-number">0.6</span>],[<spanclass="hljs-number">0.8</span>]])<spanclass="hljs-comment"># `right.shape == (3, 1)`</span>
</code></pre></div>
<div><h4id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><codeclass="python language-python hljs">left = [[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>], <spanclass="hljs-comment"># Shape: (3, 3) <- !</span>
<div><h4id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><codeclass="python language-python hljs">left = np.array([[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>], <spanclass="hljs-comment"># `left.shape == (3, 3)`</span>
<div><h3id="example-3">Example</h3><div><h4id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<codeclass="python hljs">[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>] => [<spanclass="hljs-number">1</span>, <spanclass="hljs-number">2</span>, <spanclass="hljs-number">1</span>]</code>):</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>points = np.array([<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>])
<div><h3id="example-3">Example</h3><div><h4id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<codeclass="python hljs">[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>] => [<spanclass="hljs-number">1</span>, <spanclass="hljs-number">2</span>, <spanclass="hljs-number">1</span>]</code>):</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>print(points := np.array([<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>]))