2017-04-11 | Paul Boddie | file changeset files shortlog | Added a test of attribute assignment. |
paul@809 | 1 | class C: |
paul@809 | 2 | def __init__(self, x): |
paul@809 | 3 | self.x = x |
paul@809 | 4 | |
paul@809 | 5 | def __str__(self): |
paul@809 | 6 | return "C(%r)" % self.x |
paul@809 | 7 | |
paul@809 | 8 | __repr__ = __str__ |
paul@809 | 9 | |
paul@809 | 10 | class D: |
paul@809 | 11 | x = 1 |
paul@809 | 12 | |
paul@809 | 13 | def f(): |
paul@809 | 14 | return D |
paul@809 | 15 | |
paul@809 | 16 | c = C(C(1)) # c.x.x = 1 |
paul@809 | 17 | print c # C(C(1)) |
paul@809 | 18 | print c.x # C(1) |
paul@809 | 19 | print c.x.x # 1 |
paul@809 | 20 | c.x.x = 2 |
paul@809 | 21 | print c # C(C(2)) |
paul@809 | 22 | print c.x # C(2) |
paul@809 | 23 | print c.x.x |
paul@809 | 24 | |
paul@809 | 25 | print D.x # 1 |
paul@809 | 26 | D.x = 2 |
paul@809 | 27 | print D.x # 2 |
paul@809 | 28 | D.x = C(3) |
paul@809 | 29 | print D.x # C(3) |
paul@809 | 30 | print D.x.x # 3 |
paul@809 | 31 | D.x.x = 4 |
paul@809 | 32 | print D.x.x # 4 |
paul@809 | 33 | |
paul@809 | 34 | f().x = 5 |
paul@809 | 35 | print D.x # 5 |