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

Roy Smith roy at panix.com
Sat Jul 2 09:25:46 EDT 2005


"Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> wrote:
>   class grouping:
> 
>       def __init__(self, .keep_this, .and_this, but_not_this, .but_this_again):
>           # real code right here

I'm really torn about this.  On the one hand, my first thought was "you 
shouldn't be writing constructors with arguments lists so long that this is 
a problem", but you showed a reasonable counter-example to that argument.  
I'm a big fan of DRY (Don't Repeat Yourself), sometimes expressed as "Once 
And Only Once", and your proposal lets you do that.

It also has the nice feature that it doesn't break any existing code; the 
suggested syntax is not currently legal Python.

What happens if I do:

def __init__ (self, .x, .y, .z):
   x = 0

what does the assignment x do?  Does it automatically get promoted to an 
assignment to self.x?  Does it generate an error?

The big question in my mind is not "Is this useful" (since it clearly is), 
but "Does the utility justify the cost?".  In other words, will it be used 
frequently enough to compensate for the added complexity to the language?  
I'm not convinced of that.

There have been some proposals floating around to implement a "with" 
keyword which would create implicit namespaces.  That's sort of what you're 
proposing here.  I'm not convinced either is a good idea, but if they were 
to be adopted, I'd certainly want to see the them done in a uniform, 
logically consistent way.



More information about the Python-list mailing list