Browse Source

Type and metaclasses

pull/31/head
Jure Šorn 5 years ago
parent
commit
69e31bb94c
2 changed files with 98 additions and 4 deletions
  1. 55
      README.md
  2. 47
      index.html

55
README.md

@ -226,17 +226,42 @@ def count(start, step):
Type
----
* **Everything is an object.**
* **Every object has a type.**
* **Type and class are synonymous.**
```python
<type> = type(<el>) # Or: <type> = <el>.__class__
<bool> = isinstance(<el>, <type>) # Also true if 'type' is a superclass of el's type.
```
```python
<tuple> = <type>.__bases__ # A tuple of type's parents.
<list> = <type>.mro() # Returns a list of all type's superclasses.
<bool> = issubclass(<sub_type>, <type>) # Checks if 'sub_type' is a subclass of 'type'.
```
* **Every class is a subclass and a superclass of itself.**
```python
<type> = type(<el>) # <class 'int'> / <class 'str'> / ...
>>> type('a'), 'a'.__class__, str
(<class 'str'>, <class 'str'>, <class 'str'>)
```
#### Some types do not have builtin names, so they must be imported:
```python
from types import FunctionType, MethodType, LambdaType, GeneratorType
```
### ABC-s
```python
from numbers import Integral, Rational, Real, Complex, Number
<bool> = isinstance(<el>, Number)
```
```python
<bool> = callable(<el>)
from collections.abc import Iterable, Collection, Sequence
<bool> = isinstance(<el>, Iterable)
```
@ -1514,6 +1539,32 @@ class MyClass(metaclass=MyMetaClass):
('abcde', 12345)
```
#### Type diagram ('abc' is a str, str is a type, ...):
```text
┏━━━━━━━━━┯━━━━━━━━━━━━━┓
┃ classes │ metaclasses ┃
┠─────────┼─────────────┨
┃ MyClass → MyMetaClass ┃
┃ │ ↓ ┃
┃ object ───→ type ←╮ ┃
┃ │ ↑ ╰───╯ ┃
┃ str ───────╯ ┃
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
```
#### Inheritance diagram (str inherits from object, ...):
```text
┏━━━━━━━━━┯━━━━━━━━━━━━━┓
┃ classes │ metaclasses ┃
┠─────────┼─────────────┨
┃ MyClass │ MyMetaClass ┃
┃ ↓ │ ↓ ┃
┃ object ←─── type ┃
┃ ↑ │ ┃
┃ str │ ┃
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
```
Operator
--------

47
index.html

@ -302,12 +302,33 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
(<span class="hljs-number">10</span>, <span class="hljs-number">12</span>, <span class="hljs-number">14</span>)
</code></pre>
<h2 id="type"><a href="#type" name="type">#</a>Type</h2>
<pre><code class="python language-python hljs">&lt;type&gt; = type(&lt;el&gt;) <span class="hljs-comment"># &lt;class 'int'&gt; / &lt;class 'str'&gt; / ...</span>
<ul>
<li><strong>Everything is an object.</strong></li>
<li><strong>Every object has a type.</strong></li>
<li><strong>Type and class are synonymous.</strong></li>
</ul>
<pre><code class="python language-python hljs">&lt;type&gt; = type(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;type&gt; = &lt;el&gt;.__class__</span>
&lt;bool&gt; = isinstance(&lt;el&gt;, &lt;type&gt;) <span class="hljs-comment"># Also true if 'type' is a superclass of el's type.</span>
</code></pre>
<pre><code class="python language-python hljs">&lt;tuple&gt; = &lt;type&gt;.__bases__ <span class="hljs-comment"># A tuple of type's parents.</span>
&lt;list&gt; = &lt;type&gt;.mro() <span class="hljs-comment"># Returns a list of all type's superclasses.</span>
&lt;bool&gt; = issubclass(&lt;sub_type&gt;, &lt;type&gt;) <span class="hljs-comment"># Checks if 'sub_type' is a subclass of 'type'.</span>
</code></pre>
<ul>
<li><strong>Every class is a subclass and a superclass of itself.</strong></li>
</ul>
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>type(<span class="hljs-string">'a'</span>), <span class="hljs-string">'a'</span>.__class__, str
(&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">str</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;)
</span></code></pre>
<h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not have builtin names, so they must be imported:</h4>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType
</code></pre>
<h3 id="abcs">ABC-s</h3>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> numbers <span class="hljs-keyword">import</span> Integral, Rational, Real, Complex, Number
&lt;bool&gt; = isinstance(&lt;el&gt;, Number)
</code></pre>
<pre><code class="python language-python hljs">&lt;bool&gt; = callable(&lt;el&gt;)
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Iterable, Collection, Sequence
&lt;bool&gt; = isinstance(&lt;el&gt;, Iterable)
</code></pre>
<h2 id="string"><a href="#string" name="string">#</a>String</h2>
<pre><code class="python language-python hljs">&lt;str&gt; = &lt;str&gt;.strip() <span class="hljs-comment"># Strips all whitespace characters from both ends.</span>
@ -1244,6 +1265,28 @@ param_names = list(&lt;sig&gt;.parameters.keys())
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>MyClass.a, MyClass.b
(<span class="hljs-string">'abcde'</span>, <span class="hljs-number">12345</span>)
</code></pre>
<h4 id="typediagramabcisastrstrisatype">Type diagram ('abc' is a str, str is a type, …):</h4>
<pre><code class="text language-text">┏━━━━━━━━━┯━━━━━━━━━━━━━┓
┃ classes │ metaclasses ┃
┠─────────┼─────────────┨
┃ MyClass → MyMetaClass ┃
┃ │ ↓ ┃
┃ object ───→ type ←╮ ┃
┃ │ ↑ ╰───╯ ┃
┃ str ───────╯ ┃
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
</code></pre>
<h4 id="inheritancediagramstrinheritsfromobject">Inheritance diagram (str inherits from object, …):</h4>
<pre><code class="text language-text">┏━━━━━━━━━┯━━━━━━━━━━━━━┓
┃ classes │ metaclasses ┃
┠─────────┼─────────────┨
┃ MyClass │ MyMetaClass ┃
┃ ↓ │ ↓ ┃
┃ object ←─── type ┃
┃ ↑ │ ┃
┃ str │ ┃
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
</code></pre>
<h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> add, sub, mul, truediv, floordiv, mod, pow, neg, abs
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge

Loading…
Cancel
Save