Skip to content

Commit 69e31bb

Browse files
committed
Type and metaclasses
1 parent a25708b commit 69e31bb

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,42 @@ def count(start, step):
226226

227227
Type
228228
----
229+
* **Everything is an object.**
230+
* **Every object has a type.**
231+
* **Type and class are synonymous.**
232+
233+
```python
234+
<type> = type(<el>) # Or: <type> = <el>.__class__
235+
<bool> = isinstance(<el>, <type>) # Also true if 'type' is a superclass of el's type.
236+
```
237+
238+
```python
239+
<tuple> = <type>.__bases__ # A tuple of type's parents.
240+
<list> = <type>.mro() # Returns a list of all type's superclasses.
241+
<bool> = issubclass(<sub_type>, <type>) # Checks if 'sub_type' is a subclass of 'type'.
242+
```
243+
244+
* **Every class is a subclass and a superclass of itself.**
245+
229246
```python
230-
<type> = type(<el>) # <class 'int'> / <class 'str'> / ...
247+
>>> type('a'), 'a'.__class__, str
248+
(<class 'str'>, <class 'str'>, <class 'str'>)
231249
```
232250

251+
#### Some types do not have builtin names, so they must be imported:
252+
```python
253+
from types import FunctionType, MethodType, LambdaType, GeneratorType
254+
```
255+
256+
### ABC-s
233257
```python
234258
from numbers import Integral, Rational, Real, Complex, Number
235259
<bool> = isinstance(<el>, Number)
236260
```
237261

238262
```python
239-
<bool> = callable(<el>)
263+
from collections.abc import Iterable, Collection, Sequence
264+
<bool> = isinstance(<el>, Iterable)
240265
```
241266

242267

@@ -1514,6 +1539,32 @@ class MyClass(metaclass=MyMetaClass):
15141539
('abcde', 12345)
15151540
```
15161541

1542+
#### Type diagram ('abc' is a str, str is a type, ...):
1543+
```text
1544+
┏━━━━━━━━━┯━━━━━━━━━━━━━┓
1545+
┃ classes │ metaclasses ┃
1546+
┠─────────┼─────────────┨
1547+
┃ MyClass → MyMetaClass ┃
1548+
┃ │ ↓ ┃
1549+
┃ object ───→ type ←╮ ┃
1550+
┃ │ ↑ ╰───╯ ┃
1551+
┃ str ───────╯ ┃
1552+
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
1553+
```
1554+
1555+
#### Inheritance diagram (str inherits from object, ...):
1556+
```text
1557+
┏━━━━━━━━━┯━━━━━━━━━━━━━┓
1558+
┃ classes │ metaclasses ┃
1559+
┠─────────┼─────────────┨
1560+
┃ MyClass │ MyMetaClass ┃
1561+
┃ ↓ │ ↓ ┃
1562+
┃ object ←─── type ┃
1563+
┃ ↑ │ ┃
1564+
┃ str │ ┃
1565+
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
1566+
```
1567+
15171568

15181569
Operator
15191570
--------

index.html

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,33 @@ <h2 id="generator"><a href="#generator" name="generator">#</a>Generator</h2>
302302
(<span class="hljs-number">10</span>, <span class="hljs-number">12</span>, <span class="hljs-number">14</span>)
303303
</code></pre>
304304
<h2 id="type"><a href="#type" name="type">#</a>Type</h2>
305-
<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>
305+
<ul>
306+
<li><strong>Everything is an object.</strong></li>
307+
<li><strong>Every object has a type.</strong></li>
308+
<li><strong>Type and class are synonymous.</strong></li>
309+
</ul>
310+
<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>
311+
&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>
312+
</code></pre>
313+
<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>
314+
&lt;list&gt; = &lt;type&gt;.mro() <span class="hljs-comment"># Returns a list of all type's superclasses.</span>
315+
&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>
316+
</code></pre>
317+
<ul>
318+
<li><strong>Every class is a subclass and a superclass of itself.</strong></li>
319+
</ul>
320+
<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
321+
(&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;)
322+
</span></code></pre>
323+
<h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not have builtin names, so they must be imported:</h4>
324+
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType
306325
</code></pre>
326+
<h3 id="abcs">ABC-s</h3>
307327
<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
308328
&lt;bool&gt; = isinstance(&lt;el&gt;, Number)
309329
</code></pre>
310-
<pre><code class="python language-python hljs">&lt;bool&gt; = callable(&lt;el&gt;)
330+
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections.abc <span class="hljs-keyword">import</span> Iterable, Collection, Sequence
331+
&lt;bool&gt; = isinstance(&lt;el&gt;, Iterable)
311332
</code></pre>
312333
<h2 id="string"><a href="#string" name="string">#</a>String</h2>
313334
<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 @@ <h3 id="metaclassattribute">Metaclass Attribute</h3>
12441265
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>MyClass.a, MyClass.b
12451266
(<span class="hljs-string">'abcde'</span>, <span class="hljs-number">12345</span>)
12461267
</code></pre>
1268+
<h4 id="typediagramabcisastrstrisatype">Type diagram ('abc' is a str, str is a type, …):</h4>
1269+
<pre><code class="text language-text">┏━━━━━━━━━┯━━━━━━━━━━━━━┓
1270+
┃ classes │ metaclasses ┃
1271+
┠─────────┼─────────────┨
1272+
┃ MyClass → MyMetaClass ┃
1273+
┃ │ ↓ ┃
1274+
┃ object ───→ type ←╮ ┃
1275+
┃ │ ↑ ╰───╯ ┃
1276+
┃ str ───────╯ ┃
1277+
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
1278+
</code></pre>
1279+
<h4 id="inheritancediagramstrinheritsfromobject">Inheritance diagram (str inherits from object, …):</h4>
1280+
<pre><code class="text language-text">┏━━━━━━━━━┯━━━━━━━━━━━━━┓
1281+
┃ classes │ metaclasses ┃
1282+
┠─────────┼─────────────┨
1283+
┃ MyClass │ MyMetaClass ┃
1284+
┃ ↓ │ ↓ ┃
1285+
┃ object ←─── type ┃
1286+
┃ ↑ │ ┃
1287+
┃ str │ ┃
1288+
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
1289+
</code></pre>
12471290
<h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2>
12481291
<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
12491292
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge

0 commit comments

Comments
 (0)