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 email@example.com To: Paul Moore firstname.lastname@example.org Cc: Python-Ideas email@example.com, "Steven D'Aprano" firstname.lastname@example.org Subject: Re: [Python-ideas] Augmented assignment syntax for objects. Message-ID: CAFwcP0gjg1BXeQ2pmv637bXG6+vcXiVu1QNEkdetuHjmoT_yCw@mail.gmail.com Content-Type: text/plain; charset="utf-8"
On 25 April 2017 at 03:53, Steven D'Aprano email@example.com 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.