"private" variables a.k.a. name mangling (WAS: What is print? A function?)

Simon Brunning simon.brunning at gmail.com
Tue Jan 25 05:28:02 EST 2005


On Mon, 24 Jan 2005 12:17:13 -0600, Philippe C. Martin
<philippecmartin at sbcglobal.net> wrote:
> 
> I use "__"for private variables because I must have read on net it was
> the way to do so - yet this seems to have changed - thanks:
> 
> http://www.network-theory.co.uk/docs/pytut/tut_77.html

Nope, that's still the right way to make a member 'really' private.
Stephen was pointing out a very common Python idiom - "private by
convention", and suggesting that using it would be more appropriate.

A member with a single preceding underscore is private by convention.
That is to say, there is no mechanism in place to prevent clients of
the class accessing these members, but they should consider themselves
to have been warned that they do so at their own risk.

If you take the back off the radio, the warranty is void. ;-)

I (and by inference Stephen) feel that this is a more "Pythonic"
approach. Give the programmer the information that they need, but
don't try to stop them from doing what they need to do.

-- 
Cheers,
Simon B,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list