Question about idiomatic use of _ and private stuff.

Gabriel Genellina gagsl-py at yahoo.com.ar
Fri Feb 23 19:00:22 EST 2007


En Fri, 23 Feb 2007 13:12:20 -0300, Steven W. Orr <steveo at syslang.net>  
escribió:

> I understand that two leading underscores in a class attribute make the
> attribute private. But I often see things that are coded up with one
> underscore. Unless I'm missing something, there's a idiom going on here.
>
> Why do people sometimes use one leading underscore?

Just a single leading underscore means "not for public usage".
Double leading underscore (without trailing underscores), means that  
Python will "mangle" the name in an attempt to make the name unique along  
the class hierarchy (so two classes in the same hierarchy may use the same  
attribute name without conflicting).
Double leading and trailing underscores are used by the Python interpreter  
itself.

__names have some pitfalls so better avoid them except on the specific use  
case for which they were designed.  
http://docs.python.org/ref/atom-identifiers.html

-- 
Gabriel Genellina




More information about the Python-list mailing list