"Private" Member Variables

Heather Coppersmith me at privacy.net
Fri May 28 13:45:14 EDT 2004


On Fri, 28 May 2004 09:56:43 -0700,
"Scott Brady Drummonds" <scott.b.drummonds.nospam at intel.com> wrote:

[ information hiding? ]

> ... Why not just allow direct reading and writing of the member
> variable?  Is there something here I'm missing?

Why not indeed?  No, there's nothing you're missing.  Python takes
a "we're all adults here" philosophy.  Collections of trivial
getters/setters are just clutter (and, IMHO, mean that your class
is merely encapsulating data rather than functionality; obviously,
though, there are times when that's the Right Thing).

If there's something worth "hiding," write a wrapper function,
document it, and use it.  If there's nothing worth hiding, then
don't write a wrapper function, document the members, and just
access the members directly.

You know your application as well as anyone.  You'll know (or
learn from experience) when it's time to present an interface to
larger functionality rather than just let the rest of the code
directly at your objects' members.

At worst, you can convert individual members to properties, and
run arbitrary code to access those properties, but explicit is
better than implicit.

> What are your thoughts?  How much privacy should I build into my
> code?  Should I be using variables beginning with "__" and
> accessors?  Or is that simply not necessary (or normal) in
> Python code?

Just document your intentions, and stick to your documentation.

Regards,
Heather

-- 
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli



More information about the Python-list mailing list