Why does python not have a mechanism for data hiding?

Duncan Booth duncan.booth at invalid.invalid
Sun May 25 05:25:25 EDT 2008


bearophileHUGS at lycos.com wrote:

> Ben Finney:
>>In Python, the philosophy "we're all consenting adults here" applies.<
> 
> Michael Foord:
>> They will use whatever they find, whether it is the best way to
>> achieve a goal or not. Once they start using it they will expect us to
>> maintain it - and us telling them it wasn't intended to be used by
>> them in the first place won't cut it.
> 
> So they will use the methods with one or two underscores too. And
> imply that you want to document them too.
> They don't seem adult enough, then ;-)
> Internal API hiding seems quite less useful if you don't need to let
> customers manage your code.
> 
Or if you code in C++ and they *really* need to get at something you made 
private they will still get at it. I've been there and done that: 'private' 
in languages which have it is rarely an advantage and frequently a pain.

Also note that private in C++ while it does make it harder to access 
(though not preventing it altogether) doesn't do any data hiding, so if you 
are subclassing a vendor-provided function you still have to take note of 
the private attributes to avoid naming collisions.

At least Python's convention which hides the implementation without making 
it hard to access has some benefits.



More information about the Python-list mailing list