From 80fc183d6786684b08672616981ba98b870f8cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 1 Jul 2019 20:00:49 +0200 Subject: [PATCH] Metaprograming --- README.md | 17 ++++++++++++++--- index.html | 13 ++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 84ec69a..d767a14 100644 --- a/README.md +++ b/README.md @@ -1709,7 +1709,7 @@ class MyMetaClass(type): * **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.** ### Metaclass Attribute -**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().** +**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().** ```python class MyClass(metaclass=MyMetaClass): @@ -1721,7 +1721,13 @@ class MyClass(metaclass=MyMetaClass): ('abcde', 12345) ``` -#### Type diagram (str is an instance of type, ...): +### Type Diagram +```python +type(MyClass) == MyMetaClass # MyClass is an instance of MyMetaClass. +type(MyMetaClass) == type # MyMetaClass is an instance of type. +type(type) == type # Type is an instance of type. +``` + ```text +---------+-------------+ | Classes | Metaclasses | @@ -1734,7 +1740,12 @@ class MyClass(metaclass=MyMetaClass): +---------+-------------+ ``` -#### Inheritance diagram (str is a subclass of object, ...): +### Inheritance Diagram +```python +MyClass.__base__ == object # MyClass is a subclass of object. +object.__base__ == None # Object is a subclass to no one. +``` + ```text +---------+-------------+ | Classes | Metaclasses | diff --git a/index.html b/index.html index 21f7e46..cad7495 100644 --- a/index.html +++ b/index.html @@ -1459,14 +1459,18 @@ param_names = list(<sig>.parameters.keys())
  • 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.
  • Metaclass Attribute

    -

    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().

    +

    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().

    class MyClass(metaclass=MyMetaClass):
         b = 12345
     
    >>> MyClass.a, MyClass.b
     ('abcde', 12345)
     
    -

    Type diagram (str is an instance of type, …):

    +

    Type Diagram

    +
    type(MyClass)     == MyMetaClass  # MyClass is an instance of MyMetaClass.
    +type(MyMetaClass) == type         # MyMetaClass is an instance of type.
    +type(type)        == type         # Type is an instance of type.
    +
    ┏━━━━━━━━━┯━━━━━━━━━━━━━┓
     ┃ Classes │ Metaclasses ┃
     ┠─────────┼─────────────┨
    @@ -1477,7 +1481,10 @@ param_names  = list(<sig>.parameters.keys())
     ┃   str ───────╯        ┃
     ┗━━━━━━━━━┷━━━━━━━━━━━━━┛
     
    -

    Inheritance diagram (str is a subclass of object, …):

    +

    Inheritance Diagram

    +
    MyClass.__base__  == object       # MyClass is a subclass of object.
    +object.__base__   == None         # Object is a subclass to no one.
    +
    ┏━━━━━━━━━┯━━━━━━━━━━━━━┓
     ┃ Classes │ Metaclasses ┃
     ┠─────────┼─────────────┨