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



"Andrew Koenig" <ark@xxxxxxx> 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.
.



Relevant Pages

  • Re: Philosophy of Polymorphism (was: Meyer(s) gets it wrong)
    ... I am NOT talking to a driver - I am talking to an interface in the ... Thus, it is not a case of "instance variables", unless you consider the ... you have a single object (the dispatch printer) ... browser (and its programmer) have to make do with whatever server I ...
    (comp.programming)
  • Re: Instance variable in an interface
    ... >> classes which implements that interface. ... >> public class ClassAimplements EmpInterface { ... >> public void processEmp() throws ProcessFailureException { ... > ClassB cannot share instance variables. ...
    (comp.lang.java.programmer)
  • Re: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code
    ... I think this is a bad idea, for a subtle reason. ... The names of instance variables are generally not part of a class' ... This proposed feature, whenever used, would tie a class' implementation to ... the interface of every method that uses the feature. ...
    (comp.lang.python)
  • Re: Difference between compile time and runtime reference/objects
    ... name for the practice: the Constant Interface Antipattern. ... int i = I.i + 1; ... Consider defining instance variables as private nearly always. ... then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, ...
    (comp.lang.java.programmer)
  • Re: Understanding Classes
    ... Allow the interface of a class to specify an instance variable which implements that interface: ... the Jump interface would be implemented by the jump instance variable. ... I'm undecided whether the instance variables which implement an interface should be required to be final or should be allowed to be an interface type or an abstract type, instead of required to be a concrete type. ...
    (comp.lang.java.programmer)