Why this doesn't work?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Feb 19 16:01:53 CET 2010

mk a écrit :
> Steven D'Aprano wrote:
>> On Thu, 18 Feb 2010 18:28:44 +0100, mk wrote:
>>>      nostat.__orig_get__ = nostat.__get__
>> I should point out that leading-and-trailing-double-underscore names 
>> are reserved for use by the language.
> Right... I completely missed that. I will try to change the habit.
> I am under impression that a function with no underscore in name is 
> meant to be called "publicly" on instance, like Foo().nostat, a function 
> with one underscore (and no trailing underscores) is meant to be like 
> "it's mostly intended for internal use, but you can still call it", 
> somewhat like "protected" in C++, and a function with two leading 
> underscores (and no trailing underscores) is meant as completely 
> internal to the class, not meant to be called by outsiders, somewhat 
> like "private" in C++ (I abstract from obvious point in C++ that 
> semantics of those keywords is enforced by a compiler in C++).
> Is that correct?

Mostly, yes.

- s/function/whatever/ : these rules apply to all names

- the single leading underscore denote implementation, IOW : you should 
not use it, unless you have a pretty good reason to do so AND you take 
full responsability for what may happens

- the double leading underscore is in fact mostly to protect your 
attribute from accidental overloading. It triggers a (simple) 
name-mangling operation that turns "__yourname" into 
"_Yourclassname__yourname". FWIW, this is something I must have a use 
for at most once a year, and even then most of the times because I'm a 
bit of a control freak sometimes.

More information about the Python-list mailing list