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

Émanuel Barry vgr255 at live.ca
Thu Apr 21 01:08:35 EDT 2016


Except from the fact that a lot of code requiring actual dicts (or str, list…) will break because they are no longer receiving a dict instance (or an instance of (one of) its subclasses), but a collections.UserDict instance which is also entirely unrelated to dict except that they have methods with similar names.

 

As I understand it, you prefer to put the burden of supporting collections.UserDict not on the people subclassing dict, but on the library developers expecting actual dicts. I’m sure there are a couple of built-in functions (written in C) accepting dicts that would not work if they didn’t get dicts. And getting built-in functions to accept instances of pure Python code is nontrivial and tricky at best, and requires a lot more work than it would make sense to in this case.

 

I’m -1 on the idea.

 

-Emanuel

 

From: Neil Girdhar
Sent: Thursday, April 21, 2016 12:34 AM

 

I think inheriting directly from dict is simply bad code because CPython doesn't promise that any of your overridden methods will be called.  The fact that it silently doesn't call them is an inscrutable trap.  And really, it's not much of a "punishment" to simply change your base class name.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160421/ed21966a/attachment.html>


More information about the Python-ideas mailing list