paul@230 | 1 | class C: |
paul@234 | 2 | def __init__(self): |
paul@234 | 3 | self.a = 1 |
paul@230 | 4 | def m(self, x): |
paul@230 | 5 | return x |
paul@230 | 6 | |
paul@230 | 7 | def f(obj, i): |
paul@230 | 8 | if i: |
paul@234 | 9 | return obj.m(i) # should cause access to an unbound method |
paul@234 | 10 | else: |
paul@234 | 11 | return obj.m |
paul@234 | 12 | |
paul@234 | 13 | def g(obj, i): |
paul@234 | 14 | obj.a # only provided by instances of C |
paul@234 | 15 | if i: |
paul@234 | 16 | return obj.m(i) # should use the method directly since obj is an instance |
paul@230 | 17 | else: |
paul@230 | 18 | return obj.m |
paul@230 | 19 | |
paul@230 | 20 | c = C() |
paul@234 | 21 | |
paul@234 | 22 | try: |
paul@234 | 23 | print f(C, 1) # fails |
paul@234 | 24 | except UnboundMethodInvocation: |
paul@234 | 25 | print "f(C, 1): Unbound method is not callable." |
paul@234 | 26 | |
paul@230 | 27 | fn = f(C, 0) |
paul@234 | 28 | |
paul@234 | 29 | try: |
paul@234 | 30 | print fn(2) # fails |
paul@234 | 31 | except UnboundMethodInvocation: |
paul@234 | 32 | print "fn(2): Unbound method is not callable." |
paul@234 | 33 | |
paul@234 | 34 | print get_using(fn, c)(2) # 2 |
paul@234 | 35 | print get_using(f(C, 0), c)(2) # 2 |
paul@234 | 36 | |
paul@234 | 37 | #print g(C, 1) # should fail with an error caused by a guard |
paul@234 | 38 | print g(c, 1) # 1 |
paul@234 | 39 | print g(c, 0)(3) # 3 |