paul@601 | 1 | class C: |
paul@601 | 2 | def f(self): |
paul@601 | 3 | print self |
paul@601 | 4 | return self.value() |
paul@601 | 5 | |
paul@601 | 6 | def value(self): |
paul@601 | 7 | return 123 |
paul@601 | 8 | |
paul@601 | 9 | c = C() |
paul@601 | 10 | |
paul@601 | 11 | class D: |
paul@601 | 12 | f = c.f |
paul@601 | 13 | |
paul@601 | 14 | d = D() |
paul@601 | 15 | |
paul@853 | 16 | def fn(): |
paul@853 | 17 | return 456 |
paul@853 | 18 | |
paul@853 | 19 | class E: |
paul@853 | 20 | f = fn |
paul@853 | 21 | g = C.f |
paul@853 | 22 | |
paul@853 | 23 | e = E() |
paul@853 | 24 | |
paul@856 | 25 | # Normal method access and invocation. |
paul@856 | 26 | |
paul@601 | 27 | print c.f.__name__ # f |
paul@601 | 28 | print c.f() # <__main__.C instance> |
paul@601 | 29 | # 123 |
paul@856 | 30 | |
paul@856 | 31 | # Access and call assigned bound method. |
paul@856 | 32 | |
paul@601 | 33 | print d.f.__name__ # wrapper |
paul@601 | 34 | print d.f() # <__main__.C instance> |
paul@601 | 35 | # 123 |
paul@853 | 36 | |
paul@856 | 37 | # Access and call assigned function. |
paul@856 | 38 | |
paul@853 | 39 | print e.f.__name__ # fn |
paul@853 | 40 | print e.f() # 456 |
paul@856 | 41 | |
paul@856 | 42 | # Access details of assigned method. |
paul@856 | 43 | |
paul@853 | 44 | print e.g.__name__ # f |
paul@853 | 45 | |
paul@856 | 46 | # Attempt to call method belonging to another class via an incompatible |
paul@856 | 47 | # instance. In Python, this would be an unbound method call attempt. |
paul@856 | 48 | |
paul@853 | 49 | try: |
paul@853 | 50 | print e.g() |
paul@853 | 51 | except TypeError: |
paul@853 | 52 | print "e.g(): e is an incompatible instance for E.g which is C.f" |
paul@853 | 53 | |
paul@853 | 54 | g = get_using(E.g, c) |
paul@853 | 55 | print g.__name__ # f |
paul@853 | 56 | print g() # <__main__.C instance> |
paul@853 | 57 | # 123 |