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

Michael Selik mike at selik.org
Thu Apr 21 08:50:36 EDT 2016


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

> On Thu, Apr 21, 2016 at 9:44 PM, Michael Selik <mike at selik.org> wrote:
> > On Thu, Apr 21, 2016 at 7:30 AM Steven D'Aprano <steve at pearwood.info>
> wrote:
> >>
> >> On Thu, Apr 21, 2016 at 04:36:54PM +1000, Nick Coghlan wrote:
> >>
> >> > Builtins can be extended, you just have to override all the methods
> >> > where
> >> > you want to change the return type:
> >>
> >> I wonder whether we should have a class decorator which automatically
> >> adds the appropriate methods?
> >
> >
> > If I'm not mistaken, some of the dunders need to be overridden as part of
> > the class definition and can't be added dynamically without some exec a
> la
> > namedtuple. If so, are you still interested in creating that decorator?
>
> Which ones? __new__ is already covered, and AFAIK all operator dunders
> can be injected just fine.
>

I have a vague memory of trouble with __repr__ when I once tried to make a
sort of mutable namedtuple, but now I can't reproduce the issue.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160421/1a5c691b/attachment.html>


More information about the Python-ideas mailing list