@ -816,6 +816,8 @@ player = Player(point, direction) # Returns its instance.
Imports
Imports
-------
-------
**Mechanism that makes code in one module available to another module.**
```python
```python
import <module> # Imports a built-in or '<module>.py'.
import <module> # Imports a built-in or '<module>.py'.
import <package> # Imports a built-in or '<package>/__init__.py'.
import <package> # Imports a built-in or '<package>/__init__.py'.
@ -826,7 +828,6 @@ import <package>.<module> # Imports a built-in or '<package>/<module>.py'.
* **Running `'import <package>'` does not automatically provide access to the package's modules unless they are explicitly imported in its init script.**
* **Running `'import <package>'` does not automatically provide access to the package's modules unless they are explicitly imported in its init script.**
* **Location of the file that is passed to python command serves as a root of all local imports.**
* **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>'`.**
* **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
Closure
@ -951,8 +952,10 @@ def add(x, y):
Class
Class
-----
-----
**A template for creating user-defined objects.**
```python
```python
class <name>:
class MyClass:
def __init__(self, a):
def __init__(self, a):
self.a = a
self.a = a
def __str__(self):
def __str__(self):
@ -969,6 +972,12 @@ class <name>:
* **If only repr() is defined, it will also be used for str().**
* **If only repr() is defined, it will also be used for str().**
* **Methods decorated with `'@staticmethod'` do not receive 'self' nor 'cls' as their first arg.**
* **Methods decorated with `'@staticmethod'` do not receive 'self' nor 'cls' as their first arg.**
```python
>>> obj = MyClass(1)
>>> obj.a, str(obj), repr(obj)
(1, '1', 'MyClass(1)')
```
#### Expressions that call the str() method:
#### Expressions that call the str() method:
```python
```python
print(<el>)
print(<el>)
@ -987,13 +996,6 @@ Z = dataclasses.make_dataclass('Z', ['a']); print/str/repr(Z(<el>))
**Pythonic way of implementing getters and setters.**
```python
class Person:
@property
def name(self):
return ' '.join(self._name)
@name.setter
def name(self, value):
self._name = value.split()
```
```python
>>> person = Person()
>>> person.name = '\t Guido van Rossum \n'
>>> person.name
'Guido van Rossum'
```
### Type Annotations
### Type Annotations
* **They add type hints to variables, arguments and functions (`'def f() -> <type>:'`).**
* **They add type hints to variables, arguments and functions (`'def f() -> <type>:'`).**
* **Hints are used by type checkers like [mypy](https://pypi.org/project/mypy/), data validation libraries such as [Pydantic](https://pypi.org/project/pydantic/) and lately also by [Cython](https://pypi.org/project/Cython/) compiler. However, they are not enforced by CPython interpreter.**
* **Hints are used by type checkers like [mypy](https://pypi.org/project/mypy/), data validation libraries such as [Pydantic](https://pypi.org/project/pydantic/) and lately also by [Cython](https://pypi.org/project/Cython/) compiler. However, they are not enforced by CPython interpreter.**
@ -684,18 +684,18 @@ direction = Direction.N <span class="hljs-comment">#
Player = make_dataclass(<spanclass="hljs-string">'Player'</span>, [<spanclass="hljs-string">'loc'</span>, <spanclass="hljs-string">'dir'</span>]) <spanclass="hljs-comment"># Creates a class.</span>
Player = make_dataclass(<spanclass="hljs-string">'Player'</span>, [<spanclass="hljs-string">'loc'</span>, <spanclass="hljs-string">'dir'</span>]) <spanclass="hljs-comment"># Creates a class.</span>
player = Player(point, direction) <spanclass="hljs-comment"># Returns its instance.</span>
player = Player(point, direction) <spanclass="hljs-comment"># Returns its instance.</span>
</code></pre>
</code></pre>
<div><h2id="imports"><ahref="#imports"name="imports">#</a>Imports</h2><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span><module><spanclass="hljs-comment"># Imports a built-in or '<module>.py'.</span>
<div><h2id="imports"><ahref="#imports"name="imports">#</a>Imports</h2><p><strong>Mechanism that makes code in one module available to another module.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span><module><spanclass="hljs-comment"># Imports a built-in or '<module>.py'.</span>
<spanclass="hljs-keyword">import</span><package><spanclass="hljs-comment"># Imports a built-in or '<package>/__init__.py'.</span>
<spanclass="hljs-keyword">import</span><package><spanclass="hljs-comment"># Imports a built-in or '<package>/__init__.py'.</span>
<spanclass="hljs-keyword">import</span><package>.<module><spanclass="hljs-comment"># Imports a built-in or '<package>/<module>.py'.</span>
<spanclass="hljs-keyword">import</span><package>.<module><spanclass="hljs-comment"># Imports a built-in or '<package>/<module>.py'.</span>
</code></pre></div>
</code></pre></div>
<ul>
<ul>
<li><strong>Package is a collection of modules, but it can also define its own objects.</strong></li>
<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>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>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>Location of the file that is passed to python command serves as a root of all local imports.</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>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>
</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 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>
@ -800,7 +800,7 @@ player = Player(point, direction) <span class="hljs-comment">#
<ul>
<ul>
<li><strong>Using only <codeclass="python hljs"><spanclass="hljs-string">'@debug'</span></code> to decorate the add() function would not work here, because debug would then receive the add() function as a 'print_result' argument. Decorators can however manually check if the argument they received is a function and act accordingly.</strong></li>
<li><strong>Using only <codeclass="python hljs"><spanclass="hljs-string">'@debug'</span></code> to decorate the add() function would not work here, because debug would then receive the add() function as a 'print_result' argument. Decorators can however manually check if the argument they received is a function and act accordingly.</strong></li>
<li><strong>Return value of repr() should be unambiguous and of str() readable.</strong></li>
<li><strong>Return value of repr() should be unambiguous and of str() readable.</strong></li>
<li><strong>If only repr() is defined, it will also be used for str().</strong></li>
<li><strong>If only repr() is defined, it will also be used for str().</strong></li>
<li><strong>Methods decorated with <codeclass="python hljs"><spanclass="hljs-string">'@staticmethod'</span></code> do not receive 'self' nor 'cls' as their first arg.</strong></li>
<li><strong>Methods decorated with <codeclass="python hljs"><spanclass="hljs-string">'@staticmethod'</span></code> do not receive 'self' nor 'cls' as their first arg.</strong></li>
<div><h4id="expressionsthatcallthestrmethod">Expressions that call the str() method:</h4><pre><codeclass="python language-python hljs">print(<el>)
<div><h4id="expressionsthatcallthestrmethod">Expressions that call the str() method:</h4><pre><codeclass="python language-python hljs">print(<el>)
<div><h3id="property">Property</h3><p><strong>Pythonic way of implementing getters and setters.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-class"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Person</span>:</span>
<li><strong>They add type hints to variables, arguments and functions (<codeclass="python hljs"><spanclass="hljs-string">'def f() -><type>:'</span></code>).</strong></li>
<li><strong>They add type hints to variables, arguments and functions (<codeclass="python hljs"><spanclass="hljs-string">'def f() -><type>:'</span></code>).</strong></li>
<li><strong>Hints are used by type checkers like <ahref="https://pypi.org/project/mypy/">mypy</a>, data validation libraries such as <ahref="https://pypi.org/project/pydantic/">Pydantic</a> and lately also by <ahref="https://pypi.org/project/Cython/">Cython</a> compiler. However, they are not enforced by CPython interpreter.</strong></li>
<li><strong>Hints are used by type checkers like <ahref="https://pypi.org/project/mypy/">mypy</a>, data validation libraries such as <ahref="https://pypi.org/project/pydantic/">Pydantic</a> and lately also by <ahref="https://pypi.org/project/Cython/">Cython</a> compiler. However, they are not enforced by CPython interpreter.</strong></li>
@ -903,6 +887,22 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<div><h3id="property">Property</h3><p><strong>Pythonic way of implementing getters and setters.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-class"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">Person</span>:</span>
<spanclass="hljs-string">'Guido van Rossum'</span>
</code></pre>
<div><h3id="slots">Slots</h3><p><strong>Mechanism that restricts objects to attributes listed in 'slots', reduces their memory footprint.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-class"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">MyClassWithSlots</span>:</span>
<div><h3id="slots">Slots</h3><p><strong>Mechanism that restricts objects to attributes listed in 'slots', reduces their memory footprint.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-class"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">MyClassWithSlots</span>:</span>