You might want to check out attrs (http://attrs.readthedocs.io/en/stable/). It can generate the __init__ for you, and much much more.
Date: Tue, 25 Apr 2017 14:33:49 +0200 > From: George Fischhof <geo...@fischhof.hu> > To: Paul Moore <p.f.mo...@gmail.com> > Cc: Python-Ideas <python-ideas@python.org>, "Steven D'Aprano" > <st...@pearwood.info> > Subject: Re: [Python-ideas] Augmented assignment syntax for objects. > Message-ID: > < > cafwcp0gjg1bxeq2pmv637bxg6+vcxivu1qnekdetuhjmot_...@mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > 2017. ?pr. 25. de. 10:04 ezt ?rta ("Paul Moore" <p.f.mo...@gmail.com>): > > On 25 April 2017 at 03:53, Steven D'Aprano <st...@pearwood.info> wrote: > > On Tue, Apr 25, 2017 at 02:08:05AM +0100, Erik wrote: > > > >> I often find myself writing __init__ methods of the form: > >> > >> def __init__(self, foo, bar, baz, spam, ham): > >> self.foo = foo > >> self.bar = bar > >> self.baz = baz > >> self.spam = spam > >> self.ham = ham > >> > >> This seems a little wordy and uses a lot of vertical space on the > >> screen. > > > > It does, and while both are annoying, in the grand scheme of things > > they're a very minor annoyance. After all, this is typically only an > > issue once per class, and not even every class, and vertical space is > > quite cheap. In general, the barrier for accepting new syntax is quite > > high, and "minor annoyance" generally doesn't reach it. > > I suspect that with a suitably creative use of inspect.signature() you > could write a decorator for this: > > @auto_attrs > def __init__(self, a, b, c): > # Remaining init code, called with self.a, self.b and self.c set > > I don't have time to experiment right now, but will try to find time > later. If nothing else, such a decorator would be a good prototype for > the proposed functionality, and may well be sufficient for the likely > use cases without needing a syntax change. > > Paul > >
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/