From: George Fischhof <george@fischhof.hu>
To: Paul Moore <p.f.moore@gmail.com>
Cc: Python-Ideas <python-ideas@python.org>, "Steven D'Aprano"
<steve@pearwood.info>
Subject: Re: [Python-ideas] Augmented assignment syntax for objects.
Message-ID:
<
CAFwcP0gjg1BXeQ2pmv637bXG6+vcXiVu1QNEkdetuHjmoT_yCw@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
2017. ?pr. 25. de. 10:04 ezt ?rta ("Paul Moore" <p.f.moore@gmail.com>):
On 25 April 2017 at 03:53, Steven D'Aprano <steve@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