Class design: accessing "private" members

Russell Wallace rwallace at esatclear.ie
Thu Jul 6 09:38:07 EDT 2000


Jerome Quelin wrote:
> 
> Hi all,
> 
> When designing a class, I wrote some accessors (get_var() and set_var()
> methods) for my "private" members (private in the sense that I don't want
> somebody to access them directly from outside the class.
> I was wondering if it is good practice to acceed to those private members from
> _inside_ my class, or should I use the accessors methods too?
> Well, ok, it would be slower (function calls are always more expensive), but
> isn't it more OO-well-behaviored?

Me, I don't generally bother using accessors at all; but if you find
they're a good idea, fair enough.

Presumably your reason for doing so is that you think there's a
significant chance that the attributes will be changed, and you want to
try to insulate client code from the effects of this change.  In that
case you should use them internally as well, because that will help
insulate the rest of your class code also.

Conversely, if you find it too awkward or inefficient to use them
internally, you should bear in mind the possibility that other people
will also find the accessor methods too awkward or inefficient when
using the class :)

-- 
"To summarize the summary of the summary: people are a problem."
Russell Wallace
mailto:rwallace at esatclear.ie
http://www.esatclear.ie/~rwallace



More information about the Python-list mailing list