On 05/03/2016 09:22 AM, Erik Bray wrote:
> 
> You seem to have this view that the sole purpose of getter and setter
> methods is to update a private member of some object. Am I
> understanding that correctly?  Because either I'm misunderstanding
> you, or you seem to be confused about why property was created for
> Python.
> 

That's not my view on getter/setter methods. Those are useful for doing
all sorts of things: type checking, validation, making variable
access/assignment override-able in subclasses, etc.

I'm questioning whether or not the syntactic sugar AROUND getter/setter
methods is useful. In languages with true private member variables, you
have to write boilerplate getter/setter methods all the time. There, the
trade-off is beneficial. Having some additional language syntax to
eliminate the boilerplate is worth it, even if all of your
getters/setters are trivial (which they should be).

But in python? There's no need to wrap member variables in getter/setter
methods: you can always access them. If you want to do something
nontrivial -- like ensure that you're setting some value to an integer
-- then sure, a setter method is appropriate. But now you have a setter
method that can throw a TypeError. What do we gain by making that a
@property? It changes,

  c.set_x(3)

to

  c.x = 3

which hides the fact that you're calling a method. Is it worth it? In
the "cons" column, we have

  * the new @property syntax
  * the mental overhead of having a new concept
  * the need for end-user documentation
  * the need for a policy regarding when (not) to use @property
  * the possibility of bugs like the one that started this thread

In the "pros" column, we have

  * no parentheses around the method call
  * the ability to add a docstring for c.x (if "x" were otherwise going
    to be a public member variable)

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to