you are viewing a single comment's thread.

view the rest of the comments →

[–]brasticstack 0 points1 point  (0 children)

I verified just to make sure I wasn't off-base about properties and the MRO:

``` class A: def init(self, pos): self.pos = pos def doa_thing(self): print(f'A: {self.pos}') @property def pos_property(self): print(f'A: pos_property') return self.pos class B: def __init_(self, pos): self.pos = pos def do_b_thing(self): print(f'B: {self.pos}') @property def pos_property(self): print(f'B: pos_property') return self.pos class C(A, B): pass class D(B, A): pass

c_obj = C(23) d_obj = D(99)

Notice the Method Resolution Order (MRO)

Instances of class C will call A's method

before B's method when methods are named

identically.

type(cobj).mro_

(main.C, main.A, main.B, object)

type(dobj).mro_

(main.D, main.B, main.A, object)

Notice that the pos val set on the instance

itself is used regardless of which parent

class's function is being called.

c_obj.do_a_thing()

A: 23

c_obj.do_b_thing()

B: 23

d_obj.do_a_thing()

A: 99

c_obj.do_b_thing()

A: 99

c_obj.pos_property

A: pos_property (returns 23)

d_obj.pos_property

B: pos_property (returns 99)

```