Mixins and traits 00:00:00 Use one behaviour in three unrelated objects You'll learn how to decide whether a reusable behaviour belongs in a mixin, identify what it expects from its host, and trace the actual call order when two reusable behaviours use the same method. A scooter and a pram can each take a clip-on bell. A ball cannot. Do we need to declare that a pram is a kind of scooter to give both a bell? Before answering, name the one physical feature the bell really depends on. It needs a bar to clip onto. The scooter and pram are otherwise unrelated. That is the useful shape of a mixin: add a focused behaviour to hosts that meet a small requirement. The name trait is also used for reusable behaviour in several languages; exact conflict and state rules depend on the language. 00:00:48 Attach two kits and test the joint Fit the bell to the scooter and ring it. Move the same kind of bell onto the pram; the bell works there too. Try to clip it to the ball and there is nowhere to attach it. In software, make the host requirement visible. A helper that silently assumes a database connection is like a bell that secretly needs special wiring. Now the bell and a horn demand the same socket. Bolting both on is no answer to which one should sound. Some languages require an explicit conflict resolution; others choose by method lookup order. Explain the local language rule and test the resulting behaviour. Don't assume all things called traits resolve conflicts alike. 00:01:30 Watch the comparison mixin derive an answer Imagine a score object that supplies equality and a less-than ordering by its numeric value. A comparison helper can derive other ordering operations from those primitives. Compare eight and ten. Eight is less and they are unequal, so greater-than is false. There is one place that knows what orders scores. Both players score eight. The ordering compares only points but equality compares player names as well. Can a helper safely infer every ordering relation from those two answers? Pause and decide what went wrong before blaming the mixin. The helper sees a tie according to one primitive and a difference according to the other. Decide whether a score is a points value, or a ranking entry with a player tie-breaker. Implement equality and ordering consistently for that model. A mixin multiplies the usefulness of a sound primitive and also multiplies its mistakes. 00:02:34 Trace a cooperative Python method call Here's a different use. A Python order writer combines an audit mixin, a timing mixin and a concrete writer. Its method resolution order puts audit first, timer second and the writer last. Call save on order forty-two. Audit logs the attempt, calls super, and the timer records the start before it calls the actual writer. Now remove the call to super from the timer. The audit message still appears and the method still returns. Does the real writer ever persist order forty-two? It never reaches the writer. In this cooperative design, super means the next method in this object's resolution order, not a hard-coded parent from the teacher's diagram. Test the full sequence with a recording writer and assert exactly one write. Also test the deliberate short-circuit case if skipping the write is ever legitimate. A few stateless formatting operations may make a good mixin. A network logger with connection lifecycle, retries and configuration usually makes a better collaborator passed into the writer. That lets a test substitute a recorder and avoids making every host responsible for hidden connection state. 00:03:49 Answer a conflict question An interviewer gives you two reusable behaviours with a method named label. In one, label means short display name; in the other, it means full postal address. Would choosing the first method in the lookup order settle the design question? It settles which code runs, not which promise the caller receives. Give the operations distinct names, or expose separate roles so clients ask for the intended value. If the contracts truly mean the same thing, define a single explicit implementation and test it through each interface. Name the smallest requirement on the host. Show one behaviour the mixin adds through that requirement. Then walk the exact lookup and delegation order when another mixin is present. If the answer depends on hidden state or accidental ordering, composition is often the simpler design.