abc don't play well with private method

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue May 18 11:17:35 EDT 2010


mouadino a écrit :
> Hello
> and thanx for your answer it's was very helpful
> 
> but just to clear some thinks :
> 
>> There's no such thing as a "private" attribute in Python. The
>> name-mangling mechanism invoked by "__name" is really meant to avoid
>> accidental redefinition of the attribute in a derived class.
> 
>> In this case, your attribute is expected to be redefined, so you
>> definitly don't want any name mangling here.
> 
> yes , but what i have supposed is that the name mangling will not be
> applied when you use abc especially when you decorate the function
> with abc.abstractmethod , because it's will(should) be redefined (so
> something is wrong with the abc module) .

I agree this might be a bit inconsistant - or at least that your 
expectations were rather understandable. But well, that's how it is and 
you'll have to deal with it for at least a few years.

Now I bet the name_mangling mechanism is applied in such a way that 
implementing your expectations would be rather impractical or add way to 
much overhead. Specially when almost no one ever seem to find a use for 
this "feature". As far as I'm concerned, I think I didn't used it more 
half a dozen times, for some very deep implementation detail of some 
miniframework, and mostly because I'm a bit on the paranoid side.

> 
>> Also and FWIW, the naming convention for "implementation attributes" is
>> a single leading underscore
> 
> sorry but i don't agree on this.

So you disagree with the established convention followed by (wild guess) 
about 99.99% of the regulars here and applied in most of the existing 
python code base.



> the two underscore (__) are used in
> classes level for defining private method in the python way,

The __names are used to invoke name mangling on the attribute (whatever 
type it is). It doesn't make it "private" - just a bit less subject to 
accidental override. And - I have ton insist on this - it applies to 
every attribute. FWIW, Python's "methods" are really not what you may 
think they are !-)

> and the
> one underscore (_) is used in the module level :

... to avoid export such named symbols in the case of a wildcard import 
AND if the module didn't defined an __ALL__ attribute.

The _names are used to denote implementation attribute - IOW, don't 
touch unless you're ready to pay the price.

> """Prepending a single underscore (_) has some support for protecting
> module variables and functions (not included with import * from).
> Prepending a double underscore (__) to an instance variable or method
> effectively serves to make the variable or method private to its class
> (using name mangling). """
> src:http://google-styleguide.googlecode.com/svn/trunk/pyguide.html

This is at best badly formulated and really needs a rewrite.




More information about the Python-list mailing list