paul@69 | 1 | Invocations in classic Python:
|
paul@69 | 2 |
|
paul@69 | 3 | f(1, 2, 3) # positional
|
paul@69 | 4 | f(1, 2) # positional with defaults
|
paul@69 | 5 | f(1, 2, c=3) # keywords
|
paul@69 | 6 | f(1, c=3) # keywords with defaults
|
paul@69 | 7 | f(1, 2, 3, 4) # extra positional arguments
|
paul@69 | 8 | f(1, 2, 3, d=4) # extra keyword arguments
|
paul@69 | 9 | f(1, 2, *args) # positional bundles (possibly with defaults)
|
paul@69 | 10 | f(1, 2, **kw) # keyword bundles (possibly with defaults)
|
paul@69 | 11 |
|
paul@69 | 12 | Note that f is never fixed before run-time in Python.
|
paul@69 | 13 |
|
paul@92 | 14 | Comparison to invocations in C:
|
paul@69 | 15 |
|
paul@69 | 16 | f(1, 2, 3) # positional, f known at compile-time
|
paul@69 | 17 | f(1, 2, 3) # positional, f is appropriate function pointer
|
paul@69 | 18 | # ie. (*f)(A, B, C)
|
paul@69 | 19 |
|
paul@213 | 20 | Least expensive cases (positional plus defaults):
|
paul@69 | 21 |
|
paul@109 | 22 | f(1, 2, 3) # put arguments in frame
|
paul@69 | 23 | # if f is not known, add arguments vs. parameters check
|
paul@69 | 24 | f(1, 2) # to handle defaults, introduce default "filling" where
|
paul@69 | 25 | # not enough arguments are given
|
paul@69 | 26 | # if f is not known, this is obviously done at run-time
|
paul@69 | 27 |
|
paul@213 | 28 | More expensive cases (keywords plus defaults):
|
paul@69 | 29 |
|
paul@109 | 30 | f(1, 2, c=3) # prepare frame using parameter details
|
paul@69 | 31 | # (provided c is a known parameter)
|
paul@69 | 32 | # if f is not known, this is obviously done at run-time
|
paul@69 | 33 | f(1, c=3) # as with the previous case, with default "filling" done
|
paul@69 | 34 | # where not enough arguments are given
|
paul@69 | 35 | # if f is not known, this is obviously done at run-time
|
paul@69 | 36 | # but with all defaults copied in before keywords are
|
paul@69 | 37 | # assigned (since their positions and thus the positions
|
paul@69 | 38 | # of missing parameters cannot be known)
|
paul@69 | 39 |
|
paul@213 | 40 | Awkward cases (extra arguments):
|
paul@69 | 41 |
|
paul@213 | 42 | f(1, 2, 3, 4) # put arguments in frame
|
paul@214 | 43 | # if f is not known, add arguments vs. parameters check;
|
paul@214 | 44 | # to handle superfluous arguments, make a suitable object
|
paul@214 | 45 | # and fill it with all such arguments
|
paul@213 | 46 |
|
paul@213 | 47 | Very awkward cases:
|
paul@213 | 48 |
|
paul@69 | 49 | f(1, 2, 3, d=4) # extra keyword arguments
|
paul@69 | 50 | f(1, 2, *args) # positional bundles (possibly with defaults)
|
paul@69 | 51 | f(1, 2, **kw) # keyword bundles (possibly with defaults)
|
paul@69 | 52 |
|
paul@69 | 53 | These cases require additional structures to be created, potentially at
|
paul@69 | 54 | run-time.
|
paul@92 | 55 |
|
paul@92 | 56 | Methods vs. functions:
|
paul@92 | 57 |
|
paul@92 | 58 | f(obj, 1, 2) # f known as function at compile-time:
|
paul@92 | 59 | # f(obj, 1, 2)
|
paul@92 | 60 | # f known as C.m at compile-time:
|
paul@92 | 61 | # m(obj "assert isinstance(obj, C)", 1, 2)
|
paul@98 | 62 | # f not known at compile-time:
|
paul@92 | 63 | # f(<context>, obj, 1, 2) for instance-accessed methods
|
paul@92 | 64 | # f(obj, 1, 2) for class-accessed methods
|
paul@92 | 65 | # f(obj, 1, 2) for functions
|
paul@92 | 66 |
|
paul@92 | 67 | (Could either have universal context usage even for functions, which would
|
paul@92 | 68 | ignore them, or attempt to remove contexts when functions are called.)
|
paul@92 | 69 |
|
paul@98 | 70 | Argument lists for functions:
|
paul@98 | 71 |
|
paul@98 | 72 | f(obj, 1, 2) # f known as function at compile-time
|
paul@98 | 73 |
|
paul@98 | 74 | f -> don't get any context information
|
paul@98 | 75 | obj -> argument #1
|
paul@98 | 76 | 1 -> argument #2
|
paul@98 | 77 | 2 -> argument #3
|
paul@98 | 78 |
|
paul@98 | 79 | Argument lists for methods:
|
paul@98 | 80 |
|
paul@98 | 81 | f(obj, 1, 2) # f known as C.m at compile-time (context is C)
|
paul@98 | 82 |
|
paul@98 | 83 | f -> C.m - don't get any context information
|
paul@98 | 84 | obj -> argument #1
|
paul@98 | 85 | 1 -> argument #2
|
paul@98 | 86 | 2 -> argument #3
|
paul@98 | 87 |
|
paul@98 | 88 | Argument lists for methods:
|
paul@98 | 89 |
|
paul@98 | 90 | f(obj, 1, 2) # f known as C.m at compile-time (context is an instance)
|
paul@98 | 91 |
|
paul@98 | 92 | f -> C.m
|
paul@98 | 93 | -> context is argument #1
|
paul@98 | 94 | obj -> argument #2
|
paul@98 | 95 | 1 -> argument #3
|
paul@98 | 96 | 2 -> argument #4
|
paul@98 | 97 |
|
paul@109 | 98 | Argument lists for classes:
|
paul@109 | 99 |
|
paul@109 | 100 | f(obj, 1, 2) # f known as C at compile-time
|
paul@109 | 101 |
|
paul@137 | 102 | f -> C.__init__
|
paul@137 | 103 | -> new instance is argument #1
|
paul@137 | 104 | obj -> argument #2
|
paul@137 | 105 | 1 -> argument #3
|
paul@137 | 106 | 2 -> argument #4
|
paul@137 | 107 |
|
paul@137 | 108 | The new instance must be manually provided as the result after the call.
|
paul@109 | 109 |
|
paul@98 | 110 | Argument lists for unknown callables:
|
paul@98 | 111 |
|
paul@98 | 112 | f(obj, 1, 2) # f not known at compile-time
|
paul@98 | 113 |
|
paul@98 | 114 | f -> f
|
paul@98 | 115 | -> load context for argument #1
|
paul@98 | 116 | obj -> argument #2
|
paul@98 | 117 | 1 -> argument #3
|
paul@98 | 118 | 2 -> argument #4
|
paul@98 | 119 |
|
paul@98 | 120 | Then, check the context and shift the frame if necessary:
|
paul@98 | 121 |
|
paul@98 | 122 | <context> is module or class:
|
paul@98 | 123 | (<context>, obj, 1, 2) -> (obj, 1, 2)
|
paul@98 | 124 |
|
paul@98 | 125 | <context> is instance: no change
|
paul@98 | 126 |
|
paul@137 | 127 | Argument lists in instantiators:
|
paul@137 | 128 |
|
paul@137 | 129 | f(obj, 1, 2) # f not known at compile-time
|
paul@137 | 130 |
|
paul@137 | 131 | f -> C.__new__ (known and called at run-time)
|
paul@137 | 132 |
|
paul@137 | 133 | Need to call C.__init__(<instance>, obj, 1, 2), preferably with the existing
|
paul@137 | 134 | frame:
|
paul@137 | 135 |
|
paul@137 | 136 | <insert instance before received arguments>
|
paul@137 | 137 | obj -> argument #1
|
paul@137 | 138 | 1 -> argument #2
|
paul@137 | 139 | 2 -> argument #3
|
paul@137 | 140 |
|
paul@137 | 141 | Then jump without switching frames.
|
paul@137 | 142 | It should be possible to replace the old, tentative context information in the
|
paul@137 | 143 | frame.
|
paul@137 | 144 |
|
paul@110 | 145 | Defaults for unknown callables:
|
paul@110 | 146 |
|
paul@110 | 147 | f(obj) # f not known at compile-time
|
paul@110 | 148 |
|
paul@110 | 149 | f -> f
|
paul@110 | 150 | -> load context for argument #1
|
paul@110 | 151 | obj -> argument #2
|
paul@110 | 152 |
|
paul@110 | 153 | Then, check the number of arguments and the availability of defaults against
|
paul@110 | 154 | the details provided by the callable's structure.
|
paul@110 | 155 |
|
paul@111 | 156 | Checking defaults for unknown callables:
|
paul@111 | 157 |
|
paul@111 | 158 | Approach #1 - pre-fill defaults, add arguments, check frame
|
paul@111 | 159 |
|
paul@111 | 160 | Approach #2 - add arguments, add defaults while checking frame
|
paul@111 | 161 |
|
paul@92 | 162 | Functions as methods:
|
paul@92 | 163 |
|
paul@92 | 164 | def f(x, y, z): ...
|
paul@92 | 165 | class C:
|
paul@92 | 166 | m = f
|
paul@92 | 167 | c = C()
|
paul@92 | 168 | ...
|
paul@92 | 169 | f(obj, 1, 2) # no restrictions on obj
|
paul@92 | 170 | obj.m(1, 2) # f(obj, 1, 2)
|
paul@92 | 171 | C.m(obj, 1, 2) # f(obj "assert isinstance(obj, C)", 1, 2)
|
paul@123 | 172 |
|
paul@123 | 173 | Context propagation:
|
paul@123 | 174 |
|
paul@123 | 175 | fn = C.m # has context C
|
paul@123 | 176 | fn(obj, 1, 2) # non-instance context -> explicit context required
|
paul@123 | 177 | # must perform isinstance(obj, C)
|
paul@123 | 178 | fn = c.m # table entry for m on C -> replace context
|
paul@123 | 179 | # gives context c
|
paul@123 | 180 | fn(1, 2) # instance context -> no explicit context required
|
paul@123 | 181 | # context c inserted in call
|
paul@214 | 182 |
|
paul@214 | 183 | Star parameters are a convenience:
|
paul@214 | 184 |
|
paul@214 | 185 | max(1, 2, 3) # call to max(*args) where args == (1, 2, 3)
|
paul@214 | 186 | max((1, 2, 3)) # but why not just do this instead?
|
paul@214 | 187 |
|
paul@214 | 188 | One motivation: avoid explicitly making sequences.
|
paul@214 | 189 | Opportunity: avoid expensive dynamic allocation of sequences?
|
paul@214 | 190 |
|
paul@214 | 191 | Star parameters, known callables and sequences:
|
paul@214 | 192 |
|
paul@214 | 193 | g(1, 2, 3, 4) # g known as function g(a, *args) at compile-time
|
paul@214 | 194 |
|
paul@214 | 195 | g -> don't get any context information
|
paul@214 | 196 | 1 -> argument #1
|
paul@214 | 197 | 2 -> reference to sequence containing arguments #2, #3, #4
|
paul@214 | 198 |
|
paul@214 | 199 | (This according to approach #1 described for unknown callables. With approach
|
paul@214 | 200 | #2, normal argument positioning would occur.)
|
paul@214 | 201 |
|
paul@214 | 202 | Star parameters, unknown callables:
|
paul@214 | 203 |
|
paul@214 | 204 | g(1, 2, 3, 4) # g not known at compile-time
|
paul@214 | 205 |
|
paul@214 | 206 | g -> g
|
paul@214 | 207 | -> load context for argument #1
|
paul@214 | 208 | 1 -> argument #2
|
paul@214 | 209 | 2 -> argument #3
|
paul@214 | 210 | 3 -> argument #4
|
paul@214 | 211 | 4 -> argument #5
|
paul@214 | 212 |
|
paul@214 | 213 | Then, check the context and shift the frame if necessary (described above).
|
paul@214 | 214 |
|
paul@214 | 215 | If g has a star parameter - g(a, *args) - then...
|
paul@214 | 216 |
|
paul@214 | 217 | Approach #1 - move arguments #3, #4, #5 (or shifted to #2, #3, #4) into a
|
paul@214 | 218 | sequence, adding a reference to the sequence in their place
|
paul@214 | 219 |
|
paul@214 | 220 | Approach #2 - maintain special access rules to arguments #3, #4, #5 (perhaps
|
paul@214 | 221 | shifted to #2, #3, #4) as a C-like array
|
paul@214 | 222 |
|
paul@214 | 223 | Tradeoffs for star parameter approaches:
|
paul@214 | 224 |
|
paul@214 | 225 | Approach #1 - potentially costly at run-time as arguments need moving around,
|
paul@214 | 226 | but the arguments would behave normally in functions
|
paul@214 | 227 |
|
paul@214 | 228 | Approach #2 - need to track usage of the star parameter and to possibly copy
|
paul@214 | 229 | its contents if assigned, as well as providing special access
|
paul@214 | 230 | mechanisms, but the invocation procedure would be simpler
|