[Python-ideas] Override dict.__new__ to raise if cls is not dict; do the same for str, list, etc.

Steven D'Aprano steve at pearwood.info
Wed Apr 20 23:15:43 EDT 2016


On Wed, Apr 20, 2016 at 07:51:37PM -0700, Neil Girdhar wrote:
> Sometimes users inherit from builtin types only to find that their 
> overridden methods are not called.  Instead of this being a trap for 
> unsuspecting users, I suggest overriding the __new__ method of these types 
> so that it will raise with an informative exception explaining that, e.g., 
> instead of inheriting from dict, you should inherit from UserDict.

-1

What about those who don't want or need to inherit from UserDict, and 
are perfectly happy with inheriting from dict? Why should we break their 
working code for the sake of people whose code already isn't working?

Not everyone who inherits from dict tries to override __getitem__ and 
__setitem__ and are then surprised that other methods don't call their 
overridden methods. We shouldn't punish them.


-- 
Steve


More information about the Python-ideas mailing list