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

Chris Angelico rosuav at gmail.com
Thu Apr 21 01:47:53 EDT 2016


On Thu, Apr 21, 2016 at 3:33 PM, Neil Girdhar <mistersheik at gmail.com> wrote:
>> Why should I inherit from UserDict instead? I have to import that from
>> somewhere (is it in types? collections? though presumably your error
>> message would tell me that), and then I have to contend with the fact
>> that my class is no longer a dictionary.
>
>
> Of course it's a dictionary.  It's an abc.Mapping, which is all a user of
> your class should care about.  After all, it "quacks like a duck", which is
> all that matters.

Your first sentence is in conflict with your other statements. My type
is simply *not* a dictionary. You can start raising bug reports all
over the place saying "JSON should look for abc.Mapping rather than
dict", but I'm not even sure that it should.

ChrisA


More information about the Python-ideas mailing list