Private identifiers - naming

John Roth newsgroups at jhrothjr.com
Thu May 6 12:53:20 EDT 2004


"Sridhar R" <sridharinfinity at yahoo.com> wrote in message
news:930ba99a.0405060600.17e076bc at posting.google.com...
> In python, to hide (from direct access) identifiers from outsiders
> (not current module), we usually prefix the variables with '__'.
>
> But this is usually _difficult_ to read.  Imagine a module (which is a
> common one) containing more private variables than public variables.
>
> How cryptic it would be to see text with lot of __ in front of them !!
>
> Any thoughts?

The double underscore triggers name mangling, while a single
underscore is more of a hint that this variable is protected (rather
than private.)

My take on it is that if you have that many private variables,
maybe the class is too big and needs to be split into several
classes with clearer responsibilities.

There is also a school of thought that says that classes ought
to contain more private members than public ones, some
being more than none. I don't subscribe to it, but it's part of
the landscape.

John Roth





More information about the Python-list mailing list