Skip to content

Commit 80fc183

Browse files
committed
Metaprograming
1 parent 8cb5c9b commit 80fc183

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ class MyMetaClass(type):
17091709
* **New() can also be called directly, usually from a new() method of a child class (**`def __new__(cls): return super().__new__(cls)`**), in which case init() is not called.**
17101710

17111711
### Metaclass Attribute
1712-
**When class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
1712+
**Right before a class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().**
17131713

17141714
```python
17151715
class MyClass(metaclass=MyMetaClass):
@@ -1721,7 +1721,13 @@ class MyClass(metaclass=MyMetaClass):
17211721
('abcde', 12345)
17221722
```
17231723

1724-
#### Type diagram (str is an instance of type, ...):
1724+
### Type Diagram
1725+
```python
1726+
type(MyClass) == MyMetaClass # MyClass is an instance of MyMetaClass.
1727+
type(MyMetaClass) == type # MyMetaClass is an instance of type.
1728+
type(type) == type # Type is an instance of type.
1729+
```
1730+
17251731
```text
17261732
+---------+-------------+
17271733
| Classes | Metaclasses |
@@ -1734,7 +1740,12 @@ class MyClass(metaclass=MyMetaClass):
17341740
+---------+-------------+
17351741
```
17361742

1737-
#### Inheritance diagram (str is a subclass of object, ...):
1743+
### Inheritance Diagram
1744+
```python
1745+
MyClass.__base__ == object # MyClass is a subclass of object.
1746+
object.__base__ == None # Object is a subclass to no one.
1747+
```
1748+
17381749
```text
17391750
+---------+-------------+
17401751
| Classes | Metaclasses |

index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,14 +1459,18 @@ <h4 id="or-1">Or:</h4>
14591459
<li><strong>New() can also be called directly, usually from a new() method of a child class (</strong><code class="python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__new__</span><span class="hljs-params">(cls)</span>:</span> <span class="hljs-keyword">return</span> super().__new__(cls)</code><strong>), in which case init() is not called.</strong></li>
14601460
</ul>
14611461
<h3 id="metaclassattribute">Metaclass Attribute</h3>
1462-
<p><strong>When class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p>
1462+
<p><strong>Right before a class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p>
14631463
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span><span class="hljs-params">(metaclass=MyMetaClass)</span>:</span>
14641464
b = <span class="hljs-number">12345</span>
14651465
</code></pre>
14661466
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>MyClass.a, MyClass.b
14671467
(<span class="hljs-string">'abcde'</span>, <span class="hljs-number">12345</span>)
14681468
</code></pre>
1469-
<h4 id="typediagramstrisaninstanceoftype">Type diagram (str is an instance of type, …):</h4>
1469+
<h3 id="typediagram">Type Diagram</h3>
1470+
<pre><code class="python language-python hljs">type(MyClass) == MyMetaClass <span class="hljs-comment"># MyClass is an instance of MyMetaClass.</span>
1471+
type(MyMetaClass) == type <span class="hljs-comment"># MyMetaClass is an instance of type.</span>
1472+
type(type) == type <span class="hljs-comment"># Type is an instance of type.</span>
1473+
</code></pre>
14701474
<pre><code class="text language-text">┏━━━━━━━━━┯━━━━━━━━━━━━━┓
14711475
┃ Classes │ Metaclasses ┃
14721476
┠─────────┼─────────────┨
@@ -1477,7 +1481,10 @@ <h4 id="typediagramstrisaninstanceoftype">Type diagram (str is an instance of ty
14771481
┃ str ───────╯ ┃
14781482
┗━━━━━━━━━┷━━━━━━━━━━━━━┛
14791483
</code></pre>
1480-
<h4 id="inheritancediagramstrisasubclassofobject">Inheritance diagram (str is a subclass of object, …):</h4>
1484+
<h3 id="inheritancediagram">Inheritance Diagram</h3>
1485+
<pre><code class="python language-python hljs">MyClass.__base__ == object <span class="hljs-comment"># MyClass is a subclass of object.</span>
1486+
object.__base__ == <span class="hljs-keyword">None</span> <span class="hljs-comment"># Object is a subclass to no one.</span>
1487+
</code></pre>
14811488
<pre><code class="text language-text">┏━━━━━━━━━┯━━━━━━━━━━━━━┓
14821489
┃ Classes │ Metaclasses ┃
14831490
┠─────────┼─────────────┨

0 commit comments

Comments
 (0)