2016-11-28 | Paul Boddie | file changeset files shortlog | Allow null contexts in retrieved attributes to override accessor contexts. This allows the use of functions as methods by instances. Make sure that methods which cannot be initialised in class structures are assigned with their context intact. |
paul@267 | 1 | class C: |
paul@267 | 2 | def f(self): |
paul@267 | 3 | return 1 |
paul@267 | 4 | |
paul@267 | 5 | def g(self): |
paul@267 | 6 | return self.f() |
paul@267 | 7 | |
paul@267 | 8 | class D(C): |
paul@267 | 9 | pass |
paul@267 | 10 | |
paul@267 | 11 | def f(): |
paul@267 | 12 | return 2 |
paul@267 | 13 | |
paul@267 | 14 | c = C() |
paul@267 | 15 | d = D() |
paul@267 | 16 | |
paul@267 | 17 | # Invoke a method that calls the default version of f. |
paul@267 | 18 | |
paul@267 | 19 | print c.g() # 1 |
paul@267 | 20 | print d.g() # 1 |
paul@267 | 21 | |
paul@267 | 22 | # Replace f in C and invoke the method again. For C, f will have changed, |
paul@267 | 23 | # but for D, f will retain its original value. |
paul@267 | 24 | |
paul@267 | 25 | C.f = f |
paul@267 | 26 | |
paul@267 | 27 | print c.g() # 2 |
paul@267 | 28 | print d.g() # 1 |