* **When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes `'{6.5:.0f}'` a `'6'` and `'{7.5:.0f}'` an `'8'`.**
* **This rule only works for numbers that can be represented exactly by a float (`.5`, `.25`, …).**
* **This rule only effects numbers that can be represented exactly by a float (`.5`, `.25`, …).**
* **`'int(<str>)'` and `'float(<str>)'` raise ValueError on malformed strings.**
* **All decimal numbers are stored exactly, unlike floats where `'1.1 + 2.2 != 3.3'`.**
* **Decimal numbers are stored exactly, unlike most floats where `'1.1 + 2.2 != 3.3'`.**
* **Precision of decimal operations is set with: `'decimal.getcontext().prec = <int>'`.**
### Basic Functions
@ -921,6 +921,7 @@ from functools import lru_cache
def fib(n):
return n if n <2elsefib(n-2)+fib(n-1)
```
* **Default size of the cache is 128 values. Passing `'maxsize=None'` makes it unbounded.**
* **CPython interpreter limits recursion depth to 1000 by default. To increase it use `'sys.setrecursionlimit(<depth>)'`.**
### Parametrized Decorator
@ -942,6 +943,7 @@ def debug(print_result=False):
def add(x, y):
return x + y
```
* **Using only `'@debug'` 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.**
Class
@ -974,9 +976,9 @@ raise Exception(<el>)
#### Repr() use cases:
```python
print([<el>])
print/str/repr([<el>])
f'{<el>!r}'
Z = dataclasses.make_dataclass('Z', ['a']); print(Z(<el>))
Z = dataclasses.make_dataclass('Z', ['a']); print/str/repr(Z(<el>))
<li><strong>When both rounding up and rounding down are possible, the one that returns result with even last digit is chosen. That makes <codeclass="python hljs"><spanclass="hljs-string">'{6.5:.0f}'</span></code> a <codeclass="python hljs"><spanclass="hljs-string">'6'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'{7.5:.0f}'</span></code> an <codeclass="python hljs"><spanclass="hljs-string">'8'</span></code>.</strong></li>
<li><strong>This rule only works for numbers that can be represented exactly by a float (<codeclass="python hljs"><spanclass="hljs-number">.5</span></code>, <codeclass="python hljs"><spanclass="hljs-number">.25</span></code>, …).</strong></li>
<li><strong>This rule only effects numbers that can be represented exactly by a float (<codeclass="python hljs"><spanclass="hljs-number">.5</span></code>, <codeclass="python hljs"><spanclass="hljs-number">.25</span></code>, …).</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'int(<str>)'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'float(<str>)'</span></code> raise ValueError on malformed strings.</strong></li>
<li><strong>All decimal numbers are stored exactly, unlike floats where <codeclass="python hljs"><spanclass="hljs-string">'1.1 + 2.2 != 3.3'</span></code>.</strong></li>
<li><strong>Decimal numbers are stored exactly, unlike most floats where <codeclass="python hljs"><spanclass="hljs-string">'1.1 + 2.2 != 3.3'</span></code>.</strong></li>
<li><strong>Precision of decimal operations is set with: <codeclass="python hljs"><spanclass="hljs-string">'decimal.getcontext().prec = <int>'</span></code>.</strong></li>
<li><strong>Default size of the cache is 128 values. Passing <codeclass="python hljs"><spanclass="hljs-string">'maxsize=None'</span></code> makes it unbounded.</strong></li>
<li><strong>CPython interpreter limits recursion depth to 1000 by default. To increase it use <codeclass="python hljs"><spanclass="hljs-string">'sys.setrecursionlimit(<depth>)'</span></code>.</strong></li>
</ul>
<div><h3id="parametrizeddecorator">Parametrized Decorator</h3><p><strong>A decorator that accepts arguments and returns a normal decorator that accepts a function.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> functools <spanclass="hljs-keyword">import</span> wraps
<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>