Python Inheritance: Should You Inherit From dict or UserDict? | by Marcin Kozak | May, 2023
In his fantastic book Fluent Python. 2nd ed., Luciano Ramalho explains why you should not create custom classes inheriting from dict. The reason behind this rule, strange at the first glance, is simple but critical: dict is a highly optimized type implemented in C, and it wouldn’t call the methods you overload in your subclass of dict.That would be a nasty surprise, wouldn’t it? Let’s see this in an example. Imagine you want to create a dictionary-like class in which the provided values will be converted to their string…