2.1.13 · D1OOP Fundamentals

Foundations — Abstract base classes — ABC module, `@abstractmethod`

1,620 words7 min readBack to topic

Before you can read the parent note, you need a small toolbox of ideas. We build each one from nothing, anchor it to a picture, and say why the topic needs it. Nothing here is assumed.


0. What is a "class" and an "object"? (the ground floor)

Picture a cookie cutter and cookies. The cutter is the class; each cookie stamped out is an object. One cutter → many cookies.

Figure — Abstract base classes — ABC module, `@abstractmethod`

Why the topic needs it: the entire point of an abstract base class is to stop the cutter from stamping cookies until the recipe is complete. If "class" and "object" aren't crisp in your head, "cannot be instantiated" is meaningless — so we start here.


1. Methods and self

Picture three cookies, each with its own icing colour. A method colour() uses self to know which cookie's colour to return.

Why the topic needs it: the abstract methods on the parent page (area, pay, refund) are all written as def area(self): .... You must read self as "the object this runs on" to follow every code block.


2. Inheritance — the parent/child arrow

Picture a family tree. The arrow points from child up to parent: the child is a parent, plus extra.

Figure — Abstract base classes — ABC module, `@abstractmethod`

Why the topic needs it: an ABC is a base class whose whole job is to be inherited from. "A subclass that has overridden every abstract method" (the definition of concrete) is unreadable without inheritance and override firmly in place. See Inheritance and Method Overriding for the deep version.


3. Decorators — the @name on top of a function

Think of a decorator as a sticker slapped on the box before it's shipped. The box (function) is unchanged inside, but the sticker tells the factory to treat it specially.

Figure — Abstract base classes — ABC module, `@abstractmethod`

Why the topic needs it: every abstract method on the parent page is created by a decorator. If @ reads as noise to you, you can't see what makes a method abstract.


4. Sets and the empty set

The parent page's one formula is:

Let's earn every symbol.

Picture a checklist of chores stuck to the fridge. Each time a chore is done, you cross it off. The house is "ready" iff the list is empty.

Figure — Abstract base classes — ABC module, `@abstractmethod`

Why the topic needs it: this formula IS the mechanism. Read it as "a class can make objects exactly when its leftover-chores bag is empty," and the whole parent page collapses into common sense.


5. Errors: TypeError, AttributeError, NotImplementedError

Why the topic needs it: the parent's central selling point is "move the error forward from call-time (AttributeError) to build-time (TypeError)." You must be able to tell these three apart to appreciate that shift.


How it all feeds the topic

class and object

instantiate

method and self

inheritance and override

decorator @name

@abstractmethod tags a method

ABC as a base class

set and empty set

__abstractmethods__ bag

instantiation rule: buildable iff bag empty

Abstract Base Classes

Related directions once you're solid here: Polymorphism, Duck Typing vs Nominal Typing, Interfaces in other languages, Protocols (typing.Protocol), and the Hinglish version 2.1.13 Abstract base classes — ABC module, `@abstractmethod` (Hinglish).


Equipment checklist

Cover the right side and answer aloud. If any is shaky, re-read that section before the parent note.

What is the difference between a class and an object?
A class is the template/recipe; an object is a concrete thing built from it (an instance).
What does "instantiate a class" mean in code?
Call the class like a function — Circle(2) — to build one object from it.
Inside a method, what does self refer to?
The particular object the method was called on.
What does class Circle(Shape): declare?
That Circle inherits from Shape — it is a subclass and receives Shape's methods.
What does it mean to override a method?
The subclass writes its own version, replacing the parent's version for its objects.
What is a decorator, in one line?
A @name line above a def that wraps or tags the function beneath it.
Why must @abstractmethod sit below @property?
Decorators apply bottom-up, so the innermost one tags the raw function before property wraps it.
What does the symbol mean?
The empty set — a set containing nothing.
Read in words.
"If and only if" — both sides are true at exactly the same times.
What does __abstractmethods__ hold, and when is a name removed?
The set of abstract method names not yet overridden; a name is removed when a subclass overrides that method.
Which error does an ABC raise, and when?
TypeError at construction time (when you call ClassName()), naming the missing method.
Which error would you get without an ABC when a method is missing?
AttributeError later, when the missing method is finally called.