data hiding/namespace pollution

Steven Bethard steven.bethard at gmail.com
Mon Oct 31 21:35:08 EST 2005


Alex Hunsley wrote:
> The two main versions I've encountered for data pseudo-hiding 
> (encapsulation) in python are:
> 
> method 1:
> 
> _X  - (single underscore) - just cosmetic, a convention to let someone
>       know that this data should be private.
> 
> 
> method 2:
> 
> __X - (double underscore) - mangles the name (in a predictable way).
>       Avoids name pollution.

Method 2 is also (though to a lesser degree) just cosmetic -- it doesn't 
prevent all name clashes even if you're reasonable enough not to name 
anything in the _X__xxx pattern.  I gave an example of this in an 
earlier thread on this topic[1].  The basic problem is that 
double-underscore mangling doesn't include the module name, so two 
classes in different modules with the same class names can easily mess 
with each others' "private" attributes.

STeVe

[1]http://groups.google.com/group/comp.lang.python/msg/f03183a2c01c8ecf?hl=en&



More information about the Python-list mailing list