PLAY PODCASTS
Selecting Inheritance or Composition in Python
Episode 261

Selecting Inheritance or Composition in Python

The Real Python Podcast · Real Python

August 15, 202546m 2s

Audio is streamed directly from the publisher (dts.podtrac.com) as published in their RSS feed. Play Podcasts does not host this file. Rights-holders can request removal through the copyright & takedown page.

Show Notes

<p>When considering an object-oriented programming problem, should you prefer inheritance or composition? Why wouldn&rsquo;t it just be simpler to use functions? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder&rsquo;s Weekly articles and projects.</p> <p>Christopher shares an article about structuring code using inheritance, composition, or only functions. We discuss how the piece is a good exploration of the trade-offs of each solution. Unlike the tutorials beginners typically encounter while learning the fundamentals, the article goes much deeper into the &ldquo;why&rdquo; of object-oriented programming.</p> <p>We also share several other articles and projects from the Python community, including a news roundup, processing audio in Python, reasons why you shouldn&rsquo;t call dunder methods, smuggling arbitrary data through an emoji, an HTML to markdown converter, and a library to convert Python requests into curl commands.</p> <div class="alert alert-primary" role="alert"> <p><strong>Course Spotlight:</strong> <a href="https://realpython.com/courses/single-double-underscore-naming-conventions/">Single and Double Underscore Naming Conventions in Python</a></p> <p>In this video course, you&rsquo;ll learn a few Python naming conventions involving single and double underscores (_). You&rsquo;ll learn how to use this character to differentiate between public and non-public names in APIs, write safe classes for subclassing purposes, avoid name clashes, and more.</p> </div> <p>Topics:</p> <ul> <li>00:00:00 &ndash; Introduction</li> <li>00:02:23 &ndash; Python 3.13.6 Released</li> <li>00:02:47 &ndash; Django 5.2.5 Released</li> <li>00:02:55 &ndash; Django REST Framework Release v3.16.1</li> <li>00:03:09 &ndash; Narwhals v2.0.0 Released</li> <li>00:03:22 &ndash; mypy 1.17 Released</li> <li>00:03:42 &ndash; PEP 798: Unpacking in Comprehensions</li> <li>00:04:11 &ndash; PEP 799: A Dedicated Profilers Package for Organizing Python Profiling Tools</li> <li>00:06:12 &ndash; PyPI Users Email Phishing Attack</li> <li>00:07:33 &ndash; Django in Action</li> <li>00:08:00 &ndash; Call for proposals deadline - PyCon NL</li> <li>00:08:59 &ndash; Python Audio Processing With pedalboard</li> <li>00:18:49 &ndash; Smuggling Arbitrary Data Through an Emoji</li> <li>00:21:53 &ndash; Don&rsquo;t Call Dunder Methods</li> <li>00:28:51 &ndash; Video Course Spotlight</li> <li>00:30:27 &ndash; Inheritance Over Composition, Sometimes</li> <li>00:40:03 &ndash; html-to-markdown: HTML to Markdown Converter</li> <li>00:42:20 &ndash; curlify: A library to convert Python requests request object into curl commands</li> <li>00:44:18 &ndash; transfunctions: Support Both Sync and Async</li> <li>00:45:18 &ndash; Thanks and goodbye</li> </ul> <p>Survey:</p> <ul> <li><a href="https://tally.so/r/nWbKgJ">Listener Survey - Help Shape the Future of the Real Python Podcast</a></li> </ul> <p>News:</p> <ul> <li><a href="https://docs.python.org/release/3.13.6/whatsnew/changelog.html">Python 3.13.6 Released</a></li> <li><a href="https://docs.djangoproject.com/en/5.2/releases/5.2.5/">Django 5.2.5 Released</a></li> <li><a href="https://github.com/encode/django-rest-framework/releases/tag/3.16.1?utm_campaign=Django%2BNewsletter&amp;utm_medium=email&amp;utm_source=Django_Newsletter_297">Django REST Framework Release v3.16.1</a></li> <li><a href="https://github.com/narwhals-dev/narwhals/releases/tag/v2.0.0">Narwhals v2.0.0 Released</a></li> <li><a href="https://mypy-lang.blogspot.com/2025/07/mypy-117-released.html">Mypy 1.17 Released</a></li> <li><a href="https://peps.python.org/pep-0798/">PEP 798: Unpacking in Comprehensions (Added)</a></li> <li><a href="https://peps.python.org/pep-0799/">PEP 799: A Dedicated Profilers Package for Organizing Python Profiling Tools (Added)</a></li> <li><a href="https://blog.pypi.org/posts/2025-07-28-pypi-phishing-attack/">PyPI Users Email Phishing Attack</a> &ndash; PyPI users are being targeted by an email phishing attack attempting to trick them into logging into a fake PyPI site. This post from the Security Engineer at PyPI discusses what&rsquo;s happening and what you should do about it. There&rsquo;s also a <a href="https://blog.pypi.org/posts/2025-07-31-incident-report-phishing-attack/">follow-up post</a>.</li> <li><a href="https://pycon-nl.org/call-for-papers">PyCon NL 2025 - Call for Papers</a></li> <li><a href="https://www.manning.com/books/django-in-action">Django in Action - Christopher Trudeau - Code ladjango40</a></li> </ul> <p>Topics:</p> <ul> <li><a href="https://lwn.net/Articles/1027814/">Python Audio Processing With <code>pedalboard</code></a> &ndash; The pedalboard library for Python is aimed at audio processing of various sorts, from converting between formats to adding audio effects. This post summarizes a PyCon US talk on <code>pedalboard</code> and its uses.</li> <li><a href="https://paulbutler.org/2025/smuggling-arbitrary-data-through-an-emoji/">Smuggling Arbitrary Data Through an Emoji</a> &ndash; Unicode includes flexibility through the use of variation selectors. These include the ability to change characters through a consecutive series of coding points. But, when used with code points that don&rsquo;t need them, they&rsquo;re ignored, so you can hide data in them.</li> <li><a href="https://www.pythonmorsels.com/avoid-dunder-methods/">Don&rsquo;t Call Dunder Methods</a> &ndash; It&rsquo;s best to avoid calling dunder methods. It&rsquo;s common to define dunder methods, but uncommon to call them directly.</li> <li><a href="https://death.andgravity.com/over-composition">Inheritance Over Composition, Sometimes</a> &ndash; In an older post, Adrian wrote some code using inheritance. He got questions from his readers asking why it wouldn&rsquo;t just be simpler to use functions. This post re-implements the code with inheritance, composition, and plain old functions, then compares the approaches.</li> </ul> <p>Projects:</p> <ul> <li><a href="https://github.com/Goldziher/html-to-markdown">html-to-markdown: HTML to Markdown Converter</a></li> <li><a href="https://github.com/ofw/curlify">curlify: A library to convert Python requests request object to curl command</a></li> <li><a href="https://github.com/pomponchik/transfunctions">transfunctions: Support Both Sync and Async</a></li> </ul> <p>Additional Links:</p> <ul> <li><a href="https://www.youtube.com/watch?v=kpKrsFqJgXc">Working with Audio in Python (feat. Pedalboard) - Peter Sobot - YouTube</a></li> <li><a href="https://realpython.com/python-magic-methods/">Python&rsquo;s Magic Methods: Leverage Their Power in Your Classes – Tutorial</a></li> </ul> <p>Level up your Python skills with our expert-led courses:</p> <ul> <li><a href="https://realpython.com/courses/python-class-object/">Class Concepts: Object-Oriented Programming in Python</a></li> <li><a href="https://realpython.com/courses/single-double-underscore-naming-conventions/">Single and Double Underscore Naming Conventions in Python</a></li> <li><a href="https://realpython.com/courses/python-class-inheritance/">Inheritance and Internals: Object-Oriented Programming in Python</a></li> </ul> <p><a rel="payment" href="https://realpython.com/join">Support the podcast &amp; join our community of Pythonistas</a></p>