Why does python not have a mechanism for data hiding?

Antoon Pardon apardon at forel.vub.ac.be
Wed Jun 4 03:08:45 EDT 2008


On 2008-06-03, sturlamolden <sturlamolden at yahoo.no> wrote:
> On Jun 2, 12:40 pm, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
>
>> I think you completed missed the point.
>>
>> This is just a proof of concept thing. In a real example there would
>> of course no Set en Get methods but just methods that in the course
>> of their execution would access or update the hidden attributes
>
> I have to agree with Banks here, you have not provided an example of
> data hiding. It does not discriminate between attribute access from
> within and from outside the class. You just assume that the attribute
> named 'hidden' will be left alone. Also naming it hidden is stupid as
> it is visible.

No I don't assume that hidden wil be left alone. hidden is a free
variable in a closure and thus simply can't be accessed except by
local functions that were made accessible (and some mechanism
dependant on the CPython implementation).

> What you need is a mechanism that will thrown an exception whenever an
> attribue is accessed from outside the class, but not from inside.

And my example does this. It threw an AttributeError

> The mechanism must also be impossible to override with additional
> code.

Which as far as I know it is.

-- 
Antoon Pardon



More information about the Python-list mailing list