2.1.12 · D1OOP Fundamentals

Foundations — Polymorphism — duck typing, same interface different behavior

1,579 words7 min readBack to topic

Before you can feel why "same interface, different behavior" works, you need to own every small piece of notation the parent note throws at you. We build them one at a time, from nothing, each one earning its place before the next arrives.


1. The object — a labelled box that carries its own instructions

Look at the figure. A Dog object is one box. Inside it: a spot for data, and a spot listing what it knows how to do (speak). A Cat is a different box with the same-named slot speak, but the instructions inside are different.

Figure — Polymorphism — duck typing, same interface different behavior

2. The class — the rubber stamp that makes boxes

The parentheses () after Dog mean "press the stamp now, make me one." Without them, Dog is just the stamp sitting on the desk.


3. The dot . — "reach inside this box and get that slot"

Figure — Polymorphism — duck typing, same interface different behavior

In the figure, dog.speak reaches into the dog box and finds the speak slot. What comes out is a thing you can run.


4. The method and the call () — a behavior you can trigger


5. self — how a box refers to itself


6. raise and exceptions — the "I can't do that" signal

Figure — Polymorphism — duck typing, same interface different behavior

The figure shows the fork: the dot searches for speak; if the slot exists → run it; if it's missing → AttributeError is raised.


7. The interface — the set of slots code relies on


8. MRO — the search order when boxes inherit


The prerequisite map

object = box with data plus behaviors

class = stamp that makes boxes

dot = reach into a box for a slot

method plus parens = run the behavior

self = the box the method runs on

exception and raise = it cannot do that

interface = the set of slots code uses

MRO = search order across inherited boxes

POLYMORPHISM = one call, many behaviors

Read it top to bottom: boxes and stamps come first, the dot and the call sit in the middle, and the interface plus the failure-signal plus the search order all feed the one idea at the bottom.


Equipment checklist

Test yourself — cover the right side and answer out loud before revealing.

What is an object, in one picture?
A labelled box holding both data and behaviors (instructions).
What does a class do, and what does Dog() return?
The class is a stamp/template; Dog() presses it once and returns one instance (box).
In x.speak, what does the dot . do?
Reaches into box x and looks for a slot named speak.
What's the difference between x.speak and x.speak()?
x.speak hands you the recipe object; x.speak() runs it and hands you the result.
What is self inside a method?
The specific box the method was called on, passed in automatically.
What does Python raise when you access a missing slot?
AttributeError — "no such attribute on this object."
What does "interface" mean in duck typing?
The set of methods/attributes the code actually uses — nothing formally declared.
What does the MRO decide?
The order Python searches child-then-parent classes for a slot; first match wins.
State the one core idea in a sentence.
Python asks "does x have this method?" not "what type is x?" — so any box with the right slots plugs in.

Connections