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

Roy Smith roy at panix.com
Sat Jul 2 11:25:22 EDT 2005


"Andrew Koenig" <ark at acm.org> wrote:
> In Python, unlike many other languages, the names of formal parameters are 
> part of a function's interface. For example:
> 
>     def f(x, y):
>         return x-y
> 
> Now f(3, 4) is -1 and f(y=3,x=4) is 1.
> 
> The names of instance variables are generally not part of a class' 
> interface--they are part of its implementation.
> 
> This proposed feature, whenever used, would tie a class' implementation to 
> the interface of every method that uses the feature.  As far as I can see, 
> it is impossible to use the feature without constraining the implementation 
> in this way.

While I suppose that's true from a theoretical point of view, as a 
practical matter, I don't see it being much of a big deal.  I don't think 
I've ever written an __init__ method which saved its parameters and used 
different names for the parameter and the corresponding instance variable.  
Doing so would just be confusing (at least for the kind of code I write).

Also, it doesn't really tie it in any hard and fast way.  Right now, I 
would write:

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

under the new proposal, I would write:

def __init__ (self, .x, .y, .z):
   blah

If at some time in the future, if I decided I need to change the name of 
the instance variable without changing the exposed interface, it would be 
easy enough to do:

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

I'm still not convinced we need this, but the exposed interface issue 
doesn't worry me much.



More information about the Python-list mailing list