The conflict between subclassing and composition is as old as object-oriented programming. The latest crop of languages like Go or Rust prove that you

Subclassing in Python Redux

submited by
Style Pass
2021-06-22 18:00:10

The conflict between subclassing and composition is as old as object-oriented programming. The latest crop of languages like Go or Rust prove that you don’t need subclassing to successfully write code. But what’s a pragmatic approach to subclassing in Python, specifically?

Anybody who follows me long enough knows that I’m firmly in the composition-over-inheritance camp. However, Python is designed in a way that you can’t write idiomatic code without subclassing sometimes. My goal for this article is to meditate on the question when that sometimes is and untangle my gut feelings on the topic1.

I realize this blog post is long. In fact, it’s the longest piece of prose I’ve written since my thesis in 2006. Objectively, I should’ve split it up into at least three parts. It would be better for engagement (SEO! Social media! Clicks!) and it would make it more likely that people actually read it to the end.

But I want it to stand for itself. I want it to be a distilled essence of what I’ve learned over the years. Of course, you can feel free to take as many breaks as you like – this article isn’t going anywhere!

Leave a Comment