Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code
agriff at tin.it
Sun Jul 3 11:36:10 CEST 2005
On Sat, 2 Jul 2005 03:04:09 -0700 (PDT), "Ralf W. Grosse-Kunstleve"
<rwgk at yahoo.com> wrote:
>Hi fellow Python coders,
>I often find myself writing::
> class grouping:
> def __init__(self, x, y, z):
> self.x = x
> self.y = y
> self.z = z
> # real code, finally
>This becomes a serious nuisance in complex applications with long
>argument lists, especially if long variable names are essential for
>managing the complexity. Therefore I propose that Python includes
>built-in support for reducing the ``self.x=x`` clutter.
With some help from new-style classes you can get more than
just removing the "self.x = x" clutter.
I'm not an expert of these low-level python tricks, but
you can download from http://www.gripho.it/objs.py a
small example that allows you to write
x = Float(default = 0.0, max = 1E20)
y = Float(min = 1.0)
and you can get in addition of redudancy removal also
parameter checking. You can also have an __init__
method that gets called with attributes already set up.
More information about the Python-list