"Private" Member Variables
From: Scott Brady Drummonds (scott.b.drummonds.nospam_at_intel.com)
Date: 05/28/04
- Next message: John Hunter: "Re: Non-programmer needs help with PyGTK-2.0.0"
- Previous message: Donn Cave: "Re: terminological obscurity"
- Next in thread: Matteo Dell'Amico: "Re: "Private" Member Variables"
- Reply: Matteo Dell'Amico: "Re: "Private" Member Variables"
- Reply: Steve Menard: "Re: "Private" Member Variables"
- Reply: Heather Coppersmith: "Re: "Private" Member Variables"
- Reply: Dave Brueck: "Re: "Private" Member Variables"
- Reply: Marcus von Appen: "Re: "Private" Member Variables"
- Reply: Isaac To: "Re: "Private" Member Variables"
- Reply: Alan Gauld: "Re: "Private" Member Variables"
- Reply: F. GEIGER: "Re: "Private" Member Variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: John Hunter: "Re: Non-programmer needs help with PyGTK-2.0.0"
- Previous message: Donn Cave: "Re: terminological obscurity"
- Next in thread: Matteo Dell'Amico: "Re: "Private" Member Variables"
- Reply: Matteo Dell'Amico: "Re: "Private" Member Variables"
- Reply: Steve Menard: "Re: "Private" Member Variables"
- Reply: Heather Coppersmith: "Re: "Private" Member Variables"
- Reply: Dave Brueck: "Re: "Private" Member Variables"
- Reply: Marcus von Appen: "Re: "Private" Member Variables"
- Reply: Isaac To: "Re: "Private" Member Variables"
- Reply: Alan Gauld: "Re: "Private" Member Variables"
- Reply: F. GEIGER: "Re: "Private" Member Variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|