Why does python not have a mechanism for data hiding?

Duncan Booth duncan.booth at invalid.invalid
Mon Jun 2 06:02:10 EDT 2008


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

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list