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@69 | 20 | Least expensive cases:
|
paul@69 | 21 |
|
paul@69 | 22 | f(1, 2, 3) # put arguments on stack
|
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@69 | 28 | More expensive cases:
|
paul@69 | 29 |
|
paul@69 | 30 | f(1, 2, c=3) # prepare stack 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@69 | 40 | Awkward cases:
|
paul@69 | 41 |
|
paul@69 | 42 | f(1, 2, 3, 4) # extra positional arguments
|
paul@69 | 43 | f(1, 2, 3, d=4) # extra keyword arguments
|
paul@69 | 44 | f(1, 2, *args) # positional bundles (possibly with defaults)
|
paul@69 | 45 | f(1, 2, **kw) # keyword bundles (possibly with defaults)
|
paul@69 | 46 |
|
paul@69 | 47 | These cases require additional structures to be created, potentially at
|
paul@69 | 48 | run-time.
|
paul@92 | 49 |
|
paul@92 | 50 | Methods vs. functions:
|
paul@92 | 51 |
|
paul@92 | 52 | f(obj, 1, 2) # f known as function at compile-time:
|
paul@92 | 53 | # f(obj, 1, 2)
|
paul@92 | 54 | # f known as C.m at compile-time:
|
paul@92 | 55 | # m(obj "assert isinstance(obj, C)", 1, 2)
|
paul@92 | 56 | # f not known at compiler-time:
|
paul@92 | 57 | # f(<context>, obj, 1, 2) for instance-accessed methods
|
paul@92 | 58 | # f(obj, 1, 2) for class-accessed methods
|
paul@92 | 59 | # f(obj, 1, 2) for functions
|
paul@92 | 60 |
|
paul@92 | 61 | (Could either have universal context usage even for functions, which would
|
paul@92 | 62 | ignore them, or attempt to remove contexts when functions are called.)
|
paul@92 | 63 |
|
paul@92 | 64 | Functions as methods:
|
paul@92 | 65 |
|
paul@92 | 66 | def f(x, y, z): ...
|
paul@92 | 67 | class C:
|
paul@92 | 68 | m = f
|
paul@92 | 69 | c = C()
|
paul@92 | 70 | ...
|
paul@92 | 71 | f(obj, 1, 2) # no restrictions on obj
|
paul@92 | 72 | obj.m(1, 2) # f(obj, 1, 2)
|
paul@92 | 73 | C.m(obj, 1, 2) # f(obj "assert isinstance(obj, C)", 1, 2)
|