@ -824,7 +824,9 @@ import <package>.<module> # Imports a built-in or '<package>/<module>.py'.
* **Package is a collection of modules, but it can also define its own objects.**
* **On a filesystem this corresponds to a directory of Python files with an optional init script.**
* **Running `'import <package>'` does not automatically provide access to the package's modules unless they are explicitly imported in its init script.**
* **Imports are relative to the location of file that was passed to python command, but can be made relative to their location with `'from .[…][<pkg/module>[.…]] import <obj>'`.**
* **Location of the file that is passed to python command serves as a root of all local imports.**
* **For relative imports use `'from .[…][<pkg/module>[.…]] import <obj>'`.**
* **To install your package go to its parent dir, add 'import setuptools; setuptools.setup()' to setup.py, '[options]' and 'packages = <dir>' to setup.cfg, and run `'pip3 install -e .'`.**
Closure
@ -843,9 +845,7 @@ def get_multiplier(a):
>>> multiply_by_3(10)
30
```
* **If multiple nested functions within enclosing function reference the same value, that value gets shared.**
* **To dynamically access function's first free variable use `'<function>.__closure__[0].cell_contents'`.**
* **Any value that is referenced from within multiple nested functions gets shared.**
@ -693,7 +693,9 @@ player = Player(point, direction) <span class="hljs-comment">#
<li><strong>Package is a collection of modules, but it can also define its own objects.</strong></li>
<li><strong>On a filesystem this corresponds to a directory of Python files with an optional init script.</strong></li>
<li><strong>Running <codeclass="python hljs"><spanclass="hljs-string">'import <package>'</span></code> does not automatically provide access to the package's modules unless they are explicitly imported in its init script.</strong></li>
<li><strong>Imports are relative to the location of file that was passed to python command, but can be made relative to their location with <codeclass="python hljs"><spanclass="hljs-string">'from .[…][<pkg/module>[.…]] import <obj>'</span></code>.</strong></li>
<li><strong>Location of the file that is passed to python command serves as a root of all 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>
<li><strong>To install your package go to its parent dir, add 'import setuptools; setuptools.setup()' to setup.py, '[options]' and 'packages = <dir>' to setup.cfg, and run <codeclass="python hljs"><spanclass="hljs-string">'pip3 install -e .'</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>
@ -707,8 +709,7 @@ player = Player(point, direction) <span class="hljs-comment">#
<spanclass="hljs-number">30</span>
</code></pre>
<ul>
<li><strong>If multiple nested functions within enclosing function reference the same value, that value gets shared.</strong></li>
<li><strong>To dynamically access function's first free variable use <codeclass="python hljs"><spanclass="hljs-string">'<function>.__closure__[0].cell_contents'</span></code>.</strong></li>
<li><strong>Any value that is referenced from within multiple nested functions gets shared.</strong></li>