Class design: accessing "private" members

Alex Martelli alex at magenta.com
Mon Jul 3 10:43:00 EDT 2000


Jerome Quelin <jerome.quelin at insalien.org> wrote in message
news:962626013.769258863 at news.libertysurf.fr...
> "Alex Martelli" <alex at magenta.com> wrote:
> >These are excellent arguments for always using accessors from OUTSIDE the
> >class.  Jerome, however, was talking about using them from INSIDE the
class
> >as well, which is a different issue.
> Sure, I'm always using accessors from outside.

Bravo.  One day you'll be happy you did.

> But from inside the class, I don't know which is the best. I feel like the
KISS
> rule should apply, but what do you OO gurus think of it ? How far
encapsulation
> should go ?

"Do the simplest thing that could possibly work".  For a class
not meant to be inherited from, that implies accessing the
attributes directly until and unless very specific reasons
emerge such that this "cannot possibly work" any more.  It is
quite unlikely that such reasons will ever emerge, and, if and
when they ever do, then refactoring your program is not going
to be a horribly huge cost to pay.  99+% of the time you will
enjoy the simplicity and the resulting reduction in hassles,
and the price of refactoring is not disproportionate in those
cases where it will be needed.  (The tradeoffs might be
somewhat different in other languages, but dynamic ones
such as Python strongly enable this excellent approach).


Alex






More information about the Python-list mailing list