abc don't play well with private method

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon May 17 20:36:13 EDT 2010


On Mon, 17 May 2010 06:35:11 -0700, mouadino wrote:

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

No, your expectations are wrong. Name mangling works the same no matter 
what class, whether you are using ABCs or not.


>> Also and FWIW, the naming convention for "implementation attributes" is
>> a single leading underscore
> 
> sorry but i don't agree on this. the two underscore (__) are used in
> classes level for defining private method in the python way, and the one
> underscore (_) is used in the module level :

Whether you agree or not, single underscores are commonly used as the 
convention for private methods and attributes. See PEP 8, which is 
written by Python's creator, Guido van Rossum:

http://www.python.org/dev/peps/pep-0008/




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


If that's what it says, it's wrong, because there's nothing "effective" 
about double-underscore privacy. It's a very weak, easily bypassed 
(deliberately and accidentally) form of privacy, and it complicates 
debugging.

Generally, most people here recommend you ignore leading double-
underscores and just flag your methods as "private by convention" with a 
single leading underscore.



-- 
Steven



More information about the Python-list mailing list