Re: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

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

From: Andrea Griffini <agriff@tin.it>
Date: Sun Jul 03 2005 - 11:36:10 CEST

On Sat, 2 Jul 2005 03:04:09 -0700 (PDT), "Ralf W. Grosse-Kunstleve"
<rwgk@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

    class MyClass(Object):
        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.

HTH
Andrea
Received on Thu Sep 29 16:43:25 2005