accessor/mutator functions

Dan Sommers me at privacy.net
Mon Feb 28 15:50:22 EST 2005


On 28 Feb 2005 10:30:03 GMT,
Nick Craig-Wood <nick at craig-wood.com> wrote:

> Actually I would say just access the attribute directly for both get
> and set, until it needs to do something special in which case use
> property().

> The reason why people fill their code up with boiler plate get/set
> methods is to give them the flexibility to change the implementation
> without having to change any of the users.  In python you just swap
> from direct attribute access to using property().

The reason their code is so inflexible is that they've filled their
classes with boiler plate get/set methods.

Why do users of classes need such access anyway?  If my class performs
useful functions and returns useful results, no user of my class should
care about its attributes.  If I "have to" allow access to my attributes
in order that my users be happy, then I did something else wrong when I
designed the class and its public interface in the first place.

I usually aim for this:  if users of the public interface of my class
can figure out that I changed the implementation, then I've exposed too
much.  Sure there are exceptions, but that's my basic thought process.

Sorry about the rant.

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
μ₀ × ε₀ × c² = 1



More information about the Python-list mailing list