Python’s Rib Cage

Kirby Urner
3 min readFeb 2, 2024

You’re likely sure, based on imaginary GPT-like computations, that I’m talking about a species of snake, and its rib cage — and I am, metaphorically.

What I’m actually talking about gets introduced in Grokking Python: I’m talking about the syntax of the famous computer language named after Monty Python by its Dutch inventor.

Check out the following Dog class, endowed with two of Python’s “magic methods” also known as: “dunder methods” (with “dunder” being short for “double underline”) or “special methods”.

a Dog’s rib cage

The two __ribs__ in this case are: __init__ (the birth method) and __repr__, the representer method.

What about the eat method?

giving birth to a dog instance, feeding it right away

Certainly we call it a method, an “instance method” to be clear (one involving the self), but it’s not one of those built-in dunder (“magic” or “special”) methods, where each plays a special role.

For example, Python’s built-in types, especially numeric types, make use of the __add__ method, and its role is to be triggered by use of the plus symbol ( + ). We could endow the Dog class with an __add__ method such that dog1 + dog2 would actually return something, perhaps a new Dog instance.

adding __add__

Other __ribs__: __sub__; __mul__; __contains__; __getattr__ … over fifty on file, with more sometimes being added as Python evolves.

The idea of teaching Python this way is to partake of a well-defined grammar: that of the animal kingdom as expressed using a taxonomy of genus, phylum, species.

When we get to inheritance, those commonalities will become even more clear.

But at this juncture it’s the fact that chordates, animals with backbones, come with rib cages, that suits our need for a strong mnemonic (aid to memory).

--

--