Question about idiomatic use of _ and private stuff.

Eric Brunel eric_brunel at despammed.com
Mon Feb 26 03:32:45 EST 2007


On Sun, 25 Feb 2007 22:12:52 +0100, Bruno Desthuilliers  
<bdesth.quelquechose at free.quelquepart.fr> wrote:

> Steven W. Orr a écrit :
>> I understand that two leading underscores in a class attribute make the  
>> attribute private.
>
> Nope. It doesn't make it "private", it mangles the attribute name with  
> the class name (ie : Bar.__mangled will become Bar._Bar__mangled  
> everywhere except inside Bar). This is only useful when you want to make  
> sure an attribute will not be *accidentally* accessed by a child class.  
> FWIW, I've found it of very limited use so far...

If I'm not mistaken, it was originally introduced to allow designers of  
sub-classes to use any attribute name they liked, without bothering to go  
up the whole class hierarchy to make sure this name was not already used.  
Even if Python relies far less on inheritance than other languages, class  
hierarchies are sometimes quite large. If in addition, each class has a  
lot of attributes, looking for an unused name can become long and painful.  
In this context, the double-underscore may be a blessing.

My [€£$¥]0.02...
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list