Question about idiomatic use of _ and private stuff.

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Feb 25 16:12:52 EST 2007


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...

> But I often see things that are coded up with one 
> underscore. Unless I'm missing something, there's a idiom going on here.

Yes. Single leading underscore means "this is implementation, don't mess 
with it or your on your own". It's the equivalent of "private".




More information about the Python-list mailing list