Why does python not have a mechanism for data hiding?

Antoon Pardon apardon at forel.vub.ac.be
Mon Jun 2 12:37:35 CEST 2008


On 2008-06-02, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> Antoon Pardon <apardon at forel.vub.ac.be> wrote:
>
>> If you really need it, you can do data hiding in python. It just
>> requires a bit more work.
><snip>
>> --- $ python
>> Python 2.5.2 (r252:60911, Apr 17 2008, 13:15:05) 
>> [GCC 4.2.3 (Debian 4.2.3-3)] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> From Hide import Foo
>>>>> var = Foo()
>>>>> var.GetX()
>> 0
>>>>> var.SetX(5)
>>>>> var.GetX()
>> 5
>>>>> var.x
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> AttributeError: 'Foo' object has no attribute 'x'
>>>>> var.hidden.x
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> AttributeError: 'Foo' object has no attribute 'hidden'
>>
>
> That sort of hiding isn't any more secure than the 'hiding' you get in C++.

So?

>>>> var.GetX.func_closure[0].cell_contents.x
> 5
>
> All you've done is force the user who wants to bypass it to use a longer 
> expression, and if that's your criterion for 'hiding' then just use two 
> leading underscores.

That you can find a lock pick to get at an object doesn't contradict
that the object is locked away.

I think the intention of not having these variables accesable to
the application programmer is much stronger expressed than with
two leading underscores.

Even if the current implementation of the language makes it
relatively easy to get at the information if you really want
to.

-- 
Antoon Pardon



More information about the Python-list mailing list