Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code

jcarlson at uci.edu jcarlson at uci.edu
Sat Jul 2 21:54:26 CEST 2005


Ralf W. Grosse-Kunstleve wrote:
> A shorter alternative (my personal favorite since minimally redundant)::
>
>   class grouping:
>
>       def __init__(self, .keep_this, .and_this, but_not_this, .but_this_again):
>           # real code right here

There is also the variant which I proposed on python-dev:

class grouping:
    def __init__(self, _keep_this, _and_this, but_not_this, _but this
again):
        InitAttrs(self, locals())
        #real code goes here

Essentially you replace the '.' with '_', which doesn't require a
syntax change.  Unfortunately, both are almost invisible.  It does
offer you what you want right now (without that whole waiting a year+
for Python 2.5, PEP process, etc.).

> Enhanced __slot__ semantics proposal
> ------------------------------------
>
> When ``__slots__`` are used (cool feature!) the boilerplate problem
> becomes even worse::
>
>   class grouping:
>
>       __slots__ = ["keep_this", "and_this", "but_this_again"]
>
>       def __init__(self, keep_this, and_this, but_not_this, but_this_again):
>           self.keep_this = keep_this
>           self.and_this = and_this
>           self.but_this_again = but_this_again
>           # real code, finally

There is also the AutoSlots metaclass (which I have fixed) that does
this as well.

class grouping(object):
    __metaclass__ = AutoSlots
    def __init__(self, _keep_this, _and_this, but_not_this,
_but_this_again):
        InitAttrs(self, locals())
        #real code goes here

Both AutoSlots and InitAttrs use leading underscores on the __init__
method to discover which attributes are to be __slots__, and which
should be automatically assigned.

> P.S.: If you reply to this message, please clearly separate
> naming/syntax issues from the core issue of providing built-in support
> designed to reduce clutter.

Because you don't seem to have listened in python-dev, I'll say it
here.  Not everything that reduces clutter should be syntax, and not
every function, class, and module which reduces programming time should
be builtin.  Why?


1. Expanding Python syntax bloats the language.  It increases what you
need to teach to new Python users.  In my opinion, syntax additions
should really only be considered when significant gains in readability
and writability for many users are realized, that a lack of syntax
cannot offer.

2. Expanding the Python standard library bloats the distribution.
Right now, Python is a relatively light download.  But if one were to
include the top packages in everything, the distribution would quickly
bloat to 40+ megs.  This is not an option.  Generally, the requirements
of getting code into the standard library is either a demonstrated need
for the addition, or a known best-of-breed implementation for a
commonly used module, or both.

I believe your syntax change is a non-starter.  Why?  Because I've
offered code that does essentially everything you want, without a
syntax change.  If someone happens to include AutoSlots and InitAttrs
with their code, module, what have you, and it manages to make its way
into standard Python, so be it (I place the code into the public
domain).

The code for the InitAttrs and AutoSlots mechanism are available here:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/435880

 - Josiah




More information about the Python-list mailing list