Which way to say 'private'?

Alex Martelli aleax at aleax.it
Thu Aug 21 11:51:44 EDT 2003


Daniel Klein wrote:

> There are 2 ways to indicate private members of classes, by prepending
> 1 or 2 underscore characters, the latter causing name mangling. My
> question is: When would I use which kind to indicate privacy?

You would normally use a single underscore, which is an advisory
indication of privacy.  You would use two underscores, with the
mangling they produce, when you need to ensure against any risk
of accidental conflict with other existing names in the same space.

For example, if your class injects for its own purposes attributes
in other unrelated objects, it might be quite prudent to use the
double-underscore syntax for the names of those 'alien' attributes,
otherwise name clashes are far too likely to occur.


Alex





More information about the Python-list mailing list