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@234 | 74 | f -> f (context is null)
|
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@234 | 83 | f -> C.m (context is class C)
|
paul@234 | 84 | obj -> argument #1 (must be tested against the context)
|
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@234 | 102 | f -> instantiator of C
|
paul@234 | 103 | -> (argument #1 reserved for a new instance made by the instantiator)
|
paul@137 | 104 | obj -> argument #2
|
paul@137 | 105 | 1 -> argument #3
|
paul@137 | 106 | 2 -> argument #4
|
paul@137 | 107 |
|
paul@234 | 108 | The new instance must be provided as the result of 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@234 | 122 | f is class: no change
|
paul@234 | 123 |
|
paul@234 | 124 | <context> is class:
|
paul@98 | 125 | (<context>, obj, 1, 2) -> (obj, 1, 2)
|
paul@98 | 126 |
|
paul@98 | 127 | <context> is instance: no change
|
paul@98 | 128 |
|
paul@137 | 129 | Argument lists in instantiators:
|
paul@137 | 130 |
|
paul@137 | 131 | f(obj, 1, 2) # f not known at compile-time
|
paul@137 | 132 |
|
paul@137 | 133 | f -> C.__new__ (known and called at run-time)
|
paul@230 | 134 | -> load context for argument #1
|
paul@230 | 135 | obj -> argument #2
|
paul@230 | 136 | 1 -> argument #3
|
paul@230 | 137 | 2 -> argument #4
|
paul@137 | 138 |
|
paul@137 | 139 | Need to call C.__init__(<instance>, obj, 1, 2), preferably with the existing
|
paul@137 | 140 | frame:
|
paul@137 | 141 |
|
paul@230 | 142 | *** -> instance overwrites argument #1
|
paul@230 | 143 | obj -> argument #2
|
paul@230 | 144 | 1 -> argument #3
|
paul@230 | 145 | 2 -> argument #4
|
paul@137 | 146 |
|
paul@137 | 147 | Then jump without switching frames.
|
paul@137 | 148 |
|
paul@110 | 149 | Defaults for unknown callables:
|
paul@110 | 150 |
|
paul@110 | 151 | f(obj) # f not known at compile-time
|
paul@110 | 152 |
|
paul@110 | 153 | f -> f
|
paul@110 | 154 | -> load context for argument #1
|
paul@110 | 155 | obj -> argument #2
|
paul@110 | 156 |
|
paul@110 | 157 | Then, check the number of arguments and the availability of defaults against
|
paul@110 | 158 | the details provided by the callable's structure.
|
paul@110 | 159 |
|
paul@111 | 160 | Checking defaults for unknown callables:
|
paul@111 | 161 |
|
paul@111 | 162 | Approach #1 - pre-fill defaults, add arguments, check frame
|
paul@111 | 163 |
|
paul@111 | 164 | Approach #2 - add arguments, add defaults while checking frame
|
paul@111 | 165 |
|
paul@233 | 166 | Defaults for lambda functions:
|
paul@233 | 167 |
|
paul@233 | 168 | f = lambda x, y=default: ...
|
paul@233 | 169 |
|
paul@233 | 170 | Defines instance of f with method:
|
paul@233 | 171 |
|
paul@233 | 172 | def <lambda>(<context>, x, y=default):
|
paul@233 | 173 | ...
|
paul@233 | 174 |
|
paul@233 | 175 | Where default is attribute #1.
|
paul@233 | 176 |
|
paul@233 | 177 | f(obj) # f not known at compile-time
|
paul@233 | 178 |
|
paul@233 | 179 | f -> f
|
paul@233 | 180 | -> load context for argument #1 (f, since an instance is referenced)
|
paul@233 | 181 | obj -> argument #2
|
paul@233 | 182 |
|
paul@92 | 183 | Functions as methods:
|
paul@92 | 184 |
|
paul@92 | 185 | def f(x, y, z): ...
|
paul@92 | 186 | class C:
|
paul@92 | 187 | m = f
|
paul@92 | 188 | c = C()
|
paul@92 | 189 | ...
|
paul@92 | 190 | f(obj, 1, 2) # no restrictions on obj
|
paul@92 | 191 | obj.m(1, 2) # f(obj, 1, 2)
|
paul@92 | 192 | C.m(obj, 1, 2) # f(obj "assert isinstance(obj, C)", 1, 2)
|
paul@123 | 193 |
|
paul@123 | 194 | Context propagation:
|
paul@123 | 195 |
|
paul@123 | 196 | fn = C.m # has context C
|
paul@123 | 197 | fn(obj, 1, 2) # non-instance context -> explicit context required
|
paul@123 | 198 | # must perform isinstance(obj, C)
|
paul@123 | 199 | fn = c.m # table entry for m on C -> replace context
|
paul@123 | 200 | # gives context c
|
paul@123 | 201 | fn(1, 2) # instance context -> no explicit context required
|
paul@123 | 202 | # context c inserted in call
|
paul@214 | 203 |
|
paul@214 | 204 | Star parameters are a convenience:
|
paul@214 | 205 |
|
paul@214 | 206 | max(1, 2, 3) # call to max(*args) where args == (1, 2, 3)
|
paul@214 | 207 | max((1, 2, 3)) # but why not just do this instead?
|
paul@214 | 208 |
|
paul@214 | 209 | One motivation: avoid explicitly making sequences.
|
paul@214 | 210 | Opportunity: avoid expensive dynamic allocation of sequences?
|
paul@214 | 211 |
|
paul@255 | 212 | Star parameters, approach #1:
|
paul@255 | 213 |
|
paul@255 | 214 | Make a sequence to hold the extra arguments, either in the caller for known
|
paul@255 | 215 | callables or in the function itself.
|
paul@255 | 216 |
|
paul@255 | 217 | Such a sequence would need allocating and its contents copying from the
|
paul@255 | 218 | stack.
|
paul@255 | 219 |
|
paul@255 | 220 | Star parameters, approach #2:
|
paul@255 | 221 |
|
paul@255 | 222 | Keep the extra arguments in the stack.
|
paul@255 | 223 |
|
paul@255 | 224 | Access to the star parameter would need to consider assignment to other
|
paul@255 | 225 | things and "escape situations" for the parameter:
|
paul@255 | 226 |
|
paul@255 | 227 | def f(*args):
|
paul@255 | 228 | return args # need to allocate and return the sequence
|
paul@255 | 229 |
|
paul@255 | 230 | Access to elements of the extra argument sequence would behave slightly
|
paul@255 | 231 | differently to normal sequences, but this could be identified at
|
paul@255 | 232 | compile-time.
|
paul@255 | 233 |
|
paul@255 | 234 | Star parameters, known callables and sequences, approach #1:
|
paul@214 | 235 |
|
paul@214 | 236 | g(1, 2, 3, 4) # g known as function g(a, *args) at compile-time
|
paul@214 | 237 |
|
paul@214 | 238 | g -> don't get any context information
|
paul@214 | 239 | 1 -> argument #1
|
paul@214 | 240 | 2 -> reference to sequence containing arguments #2, #3, #4
|
paul@214 | 241 |
|
paul@255 | 242 | Star parameters, known callables and sequences, approach #2:
|
paul@255 | 243 |
|
paul@255 | 244 | g(1, 2, 3, 4) # g known as function g(a, *args) at compile-time
|
paul@214 | 245 |
|
paul@255 | 246 | g -> don't get any context information
|
paul@255 | 247 | 1 -> argument #1
|
paul@255 | 248 | 2 -> argument #2
|
paul@255 | 249 | 3 -> argument #3
|
paul@255 | 250 | 4 -> argument #4
|
paul@255 | 251 |
|
paul@255 | 252 | Star parameters, unknown callables, both approach #1 and #2:
|
paul@214 | 253 |
|
paul@214 | 254 | g(1, 2, 3, 4) # g not known at compile-time
|
paul@214 | 255 |
|
paul@214 | 256 | g -> g
|
paul@214 | 257 | -> load context for argument #1
|
paul@214 | 258 | 1 -> argument #2
|
paul@214 | 259 | 2 -> argument #3
|
paul@214 | 260 | 3 -> argument #4
|
paul@214 | 261 | 4 -> argument #5
|
paul@214 | 262 |
|
paul@214 | 263 | Then, check the context and shift the frame if necessary (described above).
|
paul@214 | 264 |
|
paul@214 | 265 | If g has a star parameter - g(a, *args) - then...
|
paul@214 | 266 |
|
paul@214 | 267 | Approach #1 - move arguments #3, #4, #5 (or shifted to #2, #3, #4) into a
|
paul@214 | 268 | sequence, adding a reference to the sequence in their place
|
paul@214 | 269 |
|
paul@214 | 270 | Approach #2 - maintain special access rules to arguments #3, #4, #5 (perhaps
|
paul@214 | 271 | shifted to #2, #3, #4) as a C-like array
|
paul@214 | 272 |
|
paul@214 | 273 | Tradeoffs for star parameter approaches:
|
paul@214 | 274 |
|
paul@214 | 275 | Approach #1 - potentially costly at run-time as arguments need moving around,
|
paul@214 | 276 | but the arguments would behave normally in functions
|
paul@214 | 277 |
|
paul@214 | 278 | Approach #2 - need to track usage of the star parameter and to possibly copy
|
paul@214 | 279 | its contents if assigned, as well as providing special access
|
paul@214 | 280 | mechanisms, but the invocation procedure would be simpler
|