2017-03-07 | Paul Boddie | file changeset files shortlog | Switched to using normal, named function parameters instead of arrays. This requires a special function to convert any array-based arguments to the positioned arguments. However, parameters no longer need to be dereferenced when used in functions. Specialised macros are required to invoke functions with different numbers of parameters. Fortunately, the macros required can be deduced from the functions defined in the program. | normal-function-parameters |
paul@503 | 1 | class C: |
paul@503 | 2 | def __init__(self): |
paul@503 | 3 | self.x = 123 |
paul@503 | 4 | |
paul@503 | 5 | def f(self): |
paul@503 | 6 | return self.x |
paul@503 | 7 | |
paul@505 | 8 | class D: |
paul@505 | 9 | pass |
paul@505 | 10 | |
paul@503 | 11 | c = C() |
paul@503 | 12 | f = C.f |
paul@503 | 13 | fn = get_using(C.f, c) |
paul@503 | 14 | print fn # __main__.C.f |
paul@503 | 15 | print fn() # 123 |
paul@505 | 16 | |
paul@503 | 17 | fn = get_using(C.f, C) |
paul@503 | 18 | print fn # __main__.C.f |
paul@503 | 19 | try: |
paul@503 | 20 | print fn() # fails |
paul@503 | 21 | except UnboundMethodInvocation: |
paul@503 | 22 | print "fn(): method is unbound" |
paul@505 | 23 | |
paul@505 | 24 | d = D() |
paul@505 | 25 | try: |
paul@505 | 26 | fn = get_using(C.f, d) |
paul@505 | 27 | except TypeError: |
paul@505 | 28 | print "get_using(C.f, d): d is not compatible with C" |
paul@505 | 29 | |
paul@505 | 30 | fn = get_using(c, C.f) |
paul@505 | 31 | print fn # <__main__.C instance> |
paul@505 | 32 | try: |
paul@505 | 33 | print fn() # fails |
paul@505 | 34 | except TypeError: |
paul@505 | 35 | print "fn(): object is not callable" |