Private identifiers - naming

Peter Hansen peter at engcorp.com
Thu May 6 12:13:26 EDT 2004


Heiko Wundram wrote:

> Am Donnerstag, 6. Mai 2004 16:00 schrieb Sridhar R:
> 
>>Any thoughts?
> 
> If you find "__varname" difficult to read, don't use it. Python doesn't hide 
> (try a dir(<instance>)) or protect access to these variables anyway, so you 
> actually gain nothing using this syntax if it makes it cryptic for you.

Actually, using __varname causes the compiler to munge the name so that
it is actually _ClassName__varname, to allow subclasses to use the same
name without stepping on the parent class' variable of that name.  So
cryptic or not, you actually _do_ gain something if, for example, you
are trying to write a library for reuse and don't want to have to
document all the attributes which are used internally, just to make sure
nobody inadvertently defines a subclass which uses the same name and
buggers up the works.

It's not about _preventing_ such access, just protecting against
casual/accidental collisions.

-Peter



More information about the Python-list mailing list