Browse Source

Numbers, Decorator, Class, Duck types

pull/135/merge
Jure Šorn 3 months ago
parent
commit
86e97e638c
3 changed files with 16 additions and 15 deletions
  1. 12
      README.md
  2. 17
      index.html
  3. 2
      parse.js

12
README.md

@ -493,7 +493,7 @@ Format
Numbers
-------
```python
<int> = int(<float/str/bool>) # Or: math.floor(<float>)
<int> = int(<float/str/bool>) # Or: math.trunc(<float>)
<float> = float(<int/str/bool>) # Or: <int/float><int>
<complex> = complex(real=0, imag=0) # Or: <int/float> ± <int/float>j
<Fraction> = fractions.Fraction(0, 1) # Or: Fraction(numerator=0, denominator=1)
@ -931,7 +931,7 @@ from functools import cache
def fib(n):
return n if n < 2 else fib(n-2) + fib(n-1)
```
* **Potential problem with cache is that it can grow indefinitely. To clear its stored values run `'fib.cache_clear()'`, or use `'@lru_cache(maxsize=<int>)'` decorator instead.**
* **Potential problem with cache is that it can grow indefinitely. To clear stored return values run `'fib.cache_clear()'`, or use `'@lru_cache(maxsize=<int>)'` decorator instead.**
* **CPython interpreter limits recursion depth to 3000 by default. To increase it run `'sys.setrecursionlimit(<int>)'`.**
### Parametrized Decorator
@ -1055,9 +1055,9 @@ class <class_name>:
* **For attributes of arbitrary type use `'typing.Any'`.**
```python
<class> = make_dataclass('<class_name>', <coll_of_attribute_names>)
<class> = make_dataclass('<class_name>', <coll_of_tuples>)
<tuple> = ('<attr_name>', <type> [, <default_value>])
Point = make_dataclass('Point', ['x', 'y'])
Point = make_dataclass('Point', [('x', float), ('y', float)])
Point = make_dataclass('Point', [('x', float, 0), ('y', float, 0)])
```
### Property
@ -1293,7 +1293,7 @@ class MySequence:
```
#### Discrepancies between glossary definitions and abstract base classes:
* **Glossary on Python's website defines iterable as any object with iter() or getitem() and sequence as any object with getitem() and len(). It does not define collection.**
* **Python's glossary defines iterable as any object with iter() or getitem() and sequence as any object with getitem() and len(). It does not define collection.**
* **Passing ABC Iterable to isinstance() or issubclass() checks whether object/class has method iter(), while ABC Collection checks for iter(), contains() and len().**
### ABC Sequence

17
index.html

@ -55,7 +55,7 @@
<body>
<header>
<aside>November 7, 2024</aside>
<aside>November 13, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -444,7 +444,7 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
{<span class="hljs-number">90</span>:X} <span class="hljs-comment"># '5A'. Number 90 in uppercase hexadecimal.</span>
</code></pre></div>
<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.floor(&lt;float&gt;)</span>
<div><h2 id="numbers"><a href="#numbers" name="numbers">#</a>Numbers</h2><pre><code class="python language-python hljs">&lt;int&gt; = int(&lt;float/str/bool&gt;) <span class="hljs-comment"># Or: math.trunc(&lt;float&gt;)</span>
&lt;float&gt; = float(&lt;int/str/bool&gt;) <span class="hljs-comment"># Or: &lt;int/float&gt;&lt;int&gt;</span>
&lt;complex&gt; = complex(real=<span class="hljs-number">0</span>, imag=<span class="hljs-number">0</span>) <span class="hljs-comment"># Or: &lt;int/float&gt; ± &lt;int/float&gt;j</span>
&lt;Fraction&gt; = fractions.Fraction(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>) <span class="hljs-comment"># Or: Fraction(numerator=0, denominator=1)</span>
@ -778,7 +778,7 @@ player = Player(point, direction) <span class="hljs-comment">#
<ul>
<li><strong>Potential problem with cache is that it can grow indefinitely. To clear its stored values run <code class="python hljs"><span class="hljs-string">'fib.cache_clear()'</span></code>, or use <code class="python hljs"><span class="hljs-string">'@lru_cache(maxsize=&lt;int&gt;)'</span></code> decorator instead.</strong></li>
<li><strong>Potential problem with cache is that it can grow indefinitely. To clear stored return values run <code class="python hljs"><span class="hljs-string">'fib.cache_clear()'</span></code>, or use <code class="python hljs"><span class="hljs-string">'@lru_cache(maxsize=&lt;int&gt;)'</span></code> decorator instead.</strong></li>
<li><strong>CPython interpreter limits recursion depth to 3000 by default. To increase it run <code class="python hljs"><span class="hljs-string">'sys.setrecursionlimit(&lt;int&gt;)'</span></code>.</strong></li>
</ul>
<div><h3 id="parametrizeddecorator">Parametrized Decorator</h3><p><strong>A decorator that accepts arguments and returns a normal decorator that accepts a function.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> wraps
@ -886,9 +886,10 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<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>
<li><strong>For attributes of arbitrary type use <code class="python hljs"><span class="hljs-string">'typing.Any'</span></code>.</strong></li>
</ul>
<pre><code class="python language-python hljs">&lt;class&gt; = make_dataclass(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;coll_of_attribute_names&gt;)
&lt;class&gt; = make_dataclass(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;coll_of_tuples&gt;)
&lt;tuple&gt; = (<span class="hljs-string">'&lt;attr_name&gt;'</span>, &lt;type&gt; [, &lt;default_value&gt;])</code></pre>
<pre><code class="python language-python hljs">Point = make_dataclass(<span class="hljs-string">'Point'</span>, [<span class="hljs-string">'x'</span>, <span class="hljs-string">'y'</span>])
Point = make_dataclass(<span class="hljs-string">'Point'</span>, [(<span class="hljs-string">'x'</span>, float), (<span class="hljs-string">'y'</span>, float)])
Point = make_dataclass(<span class="hljs-string">'Point'</span>, [(<span class="hljs-string">'x'</span>, float, <span class="hljs-number">0</span>), (<span class="hljs-string">'y'</span>, float, <span class="hljs-number">0</span>)])
</code></pre>
<div><h3 id="property">Property</h3><p><strong>Pythonic way of implementing getters and setters.</strong></p><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
<span class="hljs-meta"> @property</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">name</span><span class="hljs-params">(self)</span>:</span>
@ -1098,7 +1099,7 @@ Hello World!
<div><h4 id="discrepanciesbetweenglossarydefinitionsandabstractbaseclasses">Discrepancies between glossary definitions and abstract base classes:</h4><ul>
<li><strong>Glossary on Python's website defines iterable as any object with iter() or getitem() and sequence as any object with getitem() and len(). It does not define collection.</strong></li>
<li><strong>Python's glossary defines iterable as any object with iter() or getitem() and sequence as any object with getitem() and len(). It does not define collection.</strong></li>
<li><strong>Passing ABC Iterable to isinstance() or issubclass() checks whether object/class has method iter(), while ABC Collection checks for iter(), contains() and len().</strong></li>
</ul></div>
@ -2923,7 +2924,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active
<footer>
<aside>November 7, 2024</aside>
<aside>November 13, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

2
parse.js

@ -842,7 +842,7 @@ function fixHighlights() {
$(`code:contains(@debug(print_result=True))`).html(PARAMETRIZED_DECORATOR);
$(`code:contains(print/str/repr([<obj>]))`).html(REPR_USE_CASES);
$(`code:contains((self, a=None):)`).html(CONSTRUCTOR_OVERLOADING);
$(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
//$(`code:contains(make_dataclass(\'<class_name>\')`).html(DATACLASS);
$(`code:contains(shutil.copy)`).html(SHUTIL_COPY);
$(`code:contains(os.rename)`).html(OS_RENAME);
$(`code:contains(\'<n>s\')`).html(STRUCT_FORMAT);

Loading…
Cancel
Save