1 class C: 2 def __init__(self): 3 self.a = 1 4 def m(self, x): 5 return x 6 7 class D: 8 pass 9 10 def getc(): 11 return C() 12 13 def getd(): 14 return D() 15 16 def f(obj, i): 17 if i: 18 return obj.m(i) # should cause access to an unbound method 19 else: 20 return obj.m 21 22 def g(obj, i): 23 obj.a # only provided by instances of C 24 if i: 25 return obj.m(i) # should use the method directly since obj is an instance 26 else: 27 return obj.m 28 29 def h(obj, fn): 30 if fn: 31 obj = fn() 32 obj.a # only provided by instances of C 33 return obj.m(1) 34 else: 35 return obj.m 36 37 c = C() 38 39 try: 40 print f(C, 1) # fails 41 except UnboundMethodInvocation: 42 print "f(C, 1): Unbound method is not callable." 43 44 fn = f(C, 0) 45 46 try: 47 print fn(2) # fails 48 except UnboundMethodInvocation: 49 print "fn(2): Unbound method is not callable." 50 51 print get_using(fn, c)(2) # 2 52 print get_using(f(C, 0), c)(2) # 2 53 54 try: 55 print g(C, 1) # should fail with an error caused by a guard 56 except TypeError: 57 print "g(C, 1): C is not a suitable argument." 58 59 print g(c, 1) # 1 60 print g(c, 0)(3) # 3 61 62 print h(c, getc) # 1 63 print h(c, 0)(4) # 4 64 65 try: 66 print h(c, getd) # should fail with an error caused by a guard 67 except TypeError: 68 print "h(c, getd): getd provides an unsuitable result."