Private identifiers - naming

Richard Taylor rjt-usenet at thegrindstone.me.uk
Thu May 6 12:28:00 EDT 2004


Yermat wrote:

> Sridhar R wrote:
>> In python, to hide (from direct access) identifiers from outsiders
>> (not current module), we usually prefoften 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?
> 
> Do we really need private variable ?
> 

One of the things I like about Python is that it places trust in the
programmer. No nasty compiler policeman telling me off when I break the
rules. 

So the point of a 'private' variable is to indicate to the user of a class
that the author of the class did not intend it to be accessed directly.
Personally I tend to do this by appending a single '_' to method or
identifier names. 

If a caller wishes to ignore my hint that they should not access such
variables/methods then that is up to them. This 'trust' in the programmer
often means that more use can be made of third-party packages because the
way that they are used can be 'bent' slightly by 'tweeking'. Experience has
taught me that the over zealous use of private/protected identifiers in C++
can made libraries much less flexible.

We are all adults here, no need for nasty police men.

Richard




More information about the Python-list mailing list