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