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