"Private" Member Variables

From: Scott Brady Drummonds (scott.b.drummonds.nospam_at_intel.com)
Date: 05/28/04


Date: Fri, 28 May 2004 09:56:43 -0700

Hi, everyone,

I'm still learning Python as I develop a medium-sized project. From my
previous experience with C++, I've burnt into my mind the notion of
information hiding. I'm having trouble understanding to what extent I
should follow this policy in my Python code so I thought I'd ask the group.

I read from a previous post that to attain a private-like status of member
variables, I should prefix the variable name with two underscores ("__").
This does work, but in another post someone asked me if this was really
necessary. Given that the very nature of the language precludes any
compile-time type dependencies I'm wondering if there is any benefit to
naming variables with leading underscores and providing accessor functions
like this:
class C:
  ...
  def value(self):
    return self.__value
  ...

The problems that arise from directly relying on the member variable in C++
(compile-time type dependency, as I said above) don't exist in Python. So
why provide an accessor at all? Why not just allow direct reading and
writing of the member variable? Is there something here I'm missing?

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?

Thanks,
Scott

-- 
Remove .nospam from my e-mail address to mail me.


Relevant Pages

  • Re: Simple (newbie) regular expression question
    ... > Sorry for the simple question, but I find regular ... > expressions rather intimidating. ... > (Just to spell things out, as I have seen underscores disappear ... your question Python has an initial P and doesn't have private ...
    (comp.lang.python)
  • Re: Am I the only one who would love these extentions? - Python 3.0 proposals (long)
    ... One sign that somebody has moved from "Python newbie" to "good Python ... if the underscores are not inserted at "regular" intervals -- this ... of digits between underscores as long as the usage is "consistent" ... of acceptance -- it would probably, and quite correctly, just get ...
    (comp.lang.python)
  • Re: Why does python not have a mechanism for data hiding?
    ... Now Python has both. ... support for encapsulation then it does not have encapsulation. ... hack in an otherwise elegant language. ... I don't really like putting double underscores before ...
    (comp.lang.python)
  • Re: Double underscores -- ugly?
    ... its lack of red tape and its clean syntax -- both probably due to the ... recent versions of Python, or are being addressed in Python 3000. ... I realise that double underscores make the language conceptually ... the special methods seem to have extra ...
    (comp.lang.python)
  • Re: My python annoyances so far
    ... Underscores! ... When you get right down to it, *everything* in *every* current implementation of Python is either a one or a zero. ... I know I gave up Java because, among other reasons, I found it tedious that I had to build a class method when what I really wanted was a function. ...
    (comp.lang.python)