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

Neil Girdhar mistersheik at gmail.com
Thu Apr 21 01:57:41 EDT 2016


Lol, well, I think it should.

Anyway, the cons are that yes some people's bad code won't work.  The pros
are that unsuspecting users won't fall into the trap associated with
inheriting from dict, list or set.  Of course, people on python-ideas know
the pitfalls of inheriting from builtin types.   You're not users that
benefit from this change.  It's people who don't know the problems and have
no way of finding out until they spend a day debugging why overriding some
method on a derived class of a derived class that ultimately inherits from
dict doesn't work.  Experts always want to make their own lives better.
That's the problem with a language made by experts.  New users fall into
traps that you will never fall into (again).

On Thu, Apr 21, 2016 at 1:48 AM Chris Angelico <rosuav at gmail.com> wrote:

> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/7nFQURLhlQY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160421/bca7f328/attachment.html>


More information about the Python-ideas mailing list