Private functions and inheritance

Bruno Desthuilliers bdesth.quelquechose at
Mon Jul 16 00:38:33 CEST 2007

Maciej Bliziński a écrit :
(snip the rest - already answered by at least 3 persons).

> I
> don't want to expose the __bar() function outside, but on the other
> hand i want to defer its implementation to a subclass. It seems like I
> need to make it public, doesn't it?

First, keep in mind that that Python *does not* have any notion of 
(languaged enforced) access restriction. All that the  __name thingie 
does is to mangle the name with the class name, so become 
A._A__bar. This can be useful. Sometimes. Perhaps. (FWIW, I think I've 
used it once in seven years and I'm not sure it was really necessary 
after all).

Now there's a strong convention which says that _names prefixed by a 
single underscore are implementation stuff, and that anyone messing with 
implementation stuff implicitely accepts all the possible consequences.

More information about the Python-list mailing list