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

Ethan Furman ethan at stoneleaf.us
Wed Apr 20 23:02:29 EDT 2016


On 04/20/2016 07:51 PM, 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.

How, exactly, does that help?

And even if it does help (which I doubt), you're willing to break 
currently working code that successfully subclasses dicts, strings, 
tuples, etc., that then pass through dicts, strings, tuples, etc., to 
build their new class?

No thank you.

--
~Ethan~


More information about the Python-ideas mailing list