paul@15 | 1 | Namespace Definition
|
paul@15 | 2 | ====================
|
paul@15 | 3 |
|
paul@15 | 4 | Module attributes are defined either at the module level or by global
|
paul@15 | 5 | statements.
|
paul@15 | 6 |
|
paul@15 | 7 | Class attributes are defined only within class statements.
|
paul@15 | 8 |
|
paul@15 | 9 | Instance attributes are defined only by assignments to attributes of self
|
paul@15 | 10 | within __init__ methods.
|
paul@15 | 11 |
|
paul@11 | 12 | Data Structures
|
paul@11 | 13 | ===============
|
paul@11 | 14 |
|
paul@11 | 15 | Since classes, functions and instances are all "objects", each must support
|
paul@11 | 16 | certain features and operations in the same way.
|
paul@11 | 17 |
|
paul@11 | 18 | The __class__ Attribute
|
paul@11 | 19 | -----------------------
|
paul@11 | 20 |
|
paul@11 | 21 | All objects support the __class__ attribute:
|
paul@11 | 22 |
|
paul@11 | 23 | Class: refers to the type class (type.__class__ also refers to the type class)
|
paul@11 | 24 | Function: refers to the function class
|
paul@11 | 25 | Instance: refers to the class instantiated to make the object
|
paul@11 | 26 |
|
paul@11 | 27 | Invocation
|
paul@11 | 28 | ----------
|
paul@11 | 29 |
|
paul@11 | 30 | The following actions need to be supported:
|
paul@11 | 31 |
|
paul@11 | 32 | Class: create instance, call __init__ with instance, return object
|
paul@11 | 33 | Function: call function body, return result
|
paul@11 | 34 | Instance: call __call__ method, return result
|
paul@11 | 35 |
|
paul@11 | 36 | Structure Layout
|
paul@11 | 37 | ----------------
|
paul@11 | 38 |
|
paul@11 | 39 | A suitable structure layout might be something like this:
|
paul@11 | 40 |
|
paul@11 | 41 | 0 1 2 3 4
|
paul@11 | 42 | classcode invocation __class__ attribute ...
|
paul@11 | 43 | reference reference reference
|
paul@11 | 44 |
|
paul@11 | 45 | Here, the classcode refers to the attribute lookup table for the object. Since
|
paul@11 | 46 | classes and instances share the same classcode, they might resemble the
|
paul@11 | 47 | following:
|
paul@11 | 48 |
|
paul@11 | 49 | Class C:
|
paul@11 | 50 |
|
paul@11 | 51 | 0 1 2 3 4
|
paul@11 | 52 | code for C __new__ class type attribute ...
|
paul@11 | 53 | reference reference reference
|
paul@11 | 54 |
|
paul@11 | 55 | Instance of C:
|
paul@11 | 56 |
|
paul@11 | 57 | 0 1 2 3 4
|
paul@11 | 58 | code for C C.__call__ class C attribute ...
|
paul@11 | 59 | reference reference reference
|
paul@11 | 60 | (if exists)
|
paul@11 | 61 |
|
paul@11 | 62 | The __new__ reference would lead to code consisting of the following
|
paul@11 | 63 | instructions:
|
paul@11 | 64 |
|
paul@11 | 65 | create instance for C
|
paul@11 | 66 | call C.__init__(instance, ...)
|
paul@11 | 67 | return instance
|
paul@11 | 68 |
|
paul@11 | 69 | If C has a __call__ attribute, the invocation "slot" of C instances would
|
paul@11 | 70 | refer to the same thing as C.__call__.
|
paul@11 | 71 |
|
paul@11 | 72 | For functions, the same general layout applies:
|
paul@11 | 73 |
|
paul@11 | 74 | Function f:
|
paul@11 | 75 |
|
paul@11 | 76 | 0 1 2 3 4
|
paul@11 | 77 | code for code class attribute ...
|
paul@11 | 78 | function reference function reference
|
paul@11 | 79 | reference
|
paul@11 | 80 |
|
paul@11 | 81 | Here, the code reference would lead to code for the function.
|
paul@11 | 82 |
|
paul@11 | 83 | Invocation Operation
|
paul@11 | 84 | --------------------
|
paul@11 | 85 |
|
paul@11 | 86 | Consequently, regardless of the object an invocation is always done as
|
paul@11 | 87 | follows:
|
paul@11 | 88 |
|
paul@11 | 89 | get invocation reference (at object+1)
|
paul@11 | 90 | jump to reference
|
paul@11 | 91 |
|
paul@11 | 92 | Additional preparation is necessary before the above code: positional
|
paul@11 | 93 | arguments must be saved to the parameter stack, and keyword arguments must be
|
paul@11 | 94 | resolved and saved to the appropriate position in the parameter stack.
|
paul@11 | 95 |
|
paul@11 | 96 | Attribute Operations
|
paul@11 | 97 | --------------------
|
paul@11 | 98 |
|
paul@11 | 99 | Attribute access needs to go through the attribute lookup table.
|