[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 00:33:33 EDT 2016
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.
On Wed, Apr 20, 2016 at 11:16 PM Steven D'Aprano <steve at pearwood.info>
wrote:
> 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
> _______________________________________________
> 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/9300f84b/attachment-0001.html>
More information about the Python-ideas
mailing list