Why does python not have a mechanism for data hiding?

Antoon Pardon apardon at forel.vub.ac.be
Mon Jun 2 10:14:00 EDT 2008


On 2008-06-02, Carl Banks <pavlovevidence at gmail.com> wrote:
> On Jun 2, 9:07 am, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
>> On 2008-06-02, Carl Banks <pavlovevide... at gmail.com> wrote:
>>
>>
>>
>> > On Jun 2, 6:40 am, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
>> >> On 2008-06-02, Carl Banks <pavlovevide... at gmail.com> wrote:
>>
>> >> > On Jun 2, 5:38 am, Antoon Pardon <apar... 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.
>>
>> >> >> ----------------------------- Hide.py ---------------------------------
>> >> >> class Rec(object):
>> >> >>      def __init__(__, **kwargs):
>> >> >>          for key,value in kwargs.items():
>> >> >>              setattr(__, key, value)
>>
>> >> >>      def __getitem__(self, key):
>> >> >>          return getattr(self, key)
>>
>> >> >>      def __setitem__ (self, key, val):
>> >> >>          setattr(self, key, val)
>>
>> >> >> class Foo(object):
>>
>> >> >>   def __init__(self):
>>
>> >> >>     hidden = Rec(x=0, y=0)
>>
>> >> >>     def SetX(val):
>> >> >>       hidden.x = val
>>
>> >> >>     def SetY(val):
>> >> >>       hidden.y = val
>>
>> >> >>     def GetX():
>> >> >>       return hidden.x
>>
>> >> >>     def GetY():
>> >> >>       return hidden.y
>>
>> >> >>     self.SetX = SetX
>> >> >>     self.SetY = SetY
>> >> >>     self.GetX = GetX
>> >> >>     self.GetY = GetY
>>
>> >> > In other words, it's a useless no-op.
>>
>> >> > In fact, I'd say this is even worse than useless.  Creating accessor
>> >> > functions is a sort of blessing for external use.  Knowing that there
>> >> > are accessor functions is likely to cause a user to show even less
>> >> > restraint.
>>
>> >> I think you completed missed the point.
>>
>> > I'm not sure I missed the point so much as I failed to read your mind.
>>
>> Fine with me, it is just the other side of the coin.
>>
>> >> 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
>>
>> > Fair enough, but I don't see anything in your example that suggests a
>> > way to discriminate between access from within the class and access
>> > from outside the class, which is the crucial aspect of data hiding.
>>
>> The fact is that hidden and its attributes are not accessible from
>> outside the instance. They are only accessible to the local functions
>> of __init__. By binding those local functions as atributes to the
>> instance, hidden can be modified by what for all practical purposes
>> looks like a method call, but really is a closure call.
>
> You haven't hidden the data at all, all you've done is to change the
> means of accessing it.  What difference does it make whether I write
> foo.getX() or foo.x?  Everyone in the world still has full access to
> the data.

Can't you look beyond the specific example? The GetX is just an example.
Any local function of __init__ has access to hidden and its attributes
and could manipulate them, even if the class wouldn't define getters
and setters.


> You are not realizing that only useful(**) thing about data hiding is
> that some code has access to the data, other code does not.  If you
> "hide" data equally from everyone it's just a useless spelling change.

I already explained this. The code is a proof of concept. I agree that
the example itself doesn't do anything usefull, that doesn't mean that
the concept it is trying to illustrate is useless too.


Your metaclass can be used to make attributes private. Your metaclass
doesn't stop doing that because someone makes a class with a getter
and a setter for those private attributes. The same goes for my example.
The closure makes a variable inaccessable to the outside. That doesn't
change because I defined a getter and a setter for that closure
variable.

-- 
Antoon Pardon



More information about the Python-list mailing list