[Tutor] Data hiding in Python.

Kent Johnson kent37 at tds.net
Tue Dec 19 19:26:14 CET 2006


Don Taylor wrote:
> I am working my way through 'wxPython in Action' by Noel Rappin and 
> Robin Dunn and came across this comment about data hiding in their 
> explanation of the MVC pattern:
> 
> ".. the View... should never get to see the private internals of the 
> Model.  Admittedly, this is difficult to enforce in Python, but one way 
> to help enforcement it is to create an abstract Model class that defines 
> the API that the View can see.  Subclasses of the Model can either act 
> as proxies for an internal class that can be changed, or can simply 
> contain the internal workings themselves.  The first option is more 
> structured, the second easier to implement."
> 
> I like the idea of data hiding but Googling 'Python data hiding' yields 
> lots of discussion to the effect that it cannot/should not be done.

Python culture tends towards "we're all consenting adults here". If you 
attempt to shoot yourself in the foot, you should get some kind of 
warning that perhaps it is not what you really want to do, but if you 
insist, hey, go ahead, it's your foot!

With regard to data hiding this means using leading underscore for names 
that are considered implementation details, this is fair warning to 
clients to use them at your own risk.

> So what do the authors mean?  They do not give any examples of this 
> technique (at least not so far as I have read in the book) so I have 
> been trying to figure something out.  And then, is it worth the trouble?

My vote is, no, not worth the trouble. It's Java / C++ / static-typing / 
put-the-client-in-a-straightjacket-so-they-don't-touch-anything thinking.

> Any suggestions?  Preferably with code fragments.
> 
> Here is what I have got so far:
> 
> A calling module:
> -caller.py---------------------------------------------------------------------
> 
> import model
> 
> if __name__ == '__main__':
>      model.methodA() # test a method call interface
>      testInstance = model.TestClass() #test a class generation interface
>      testInstance.methodB() # test a method call within the new class
> -------------------------------------------------------------------------------
> 
> The actual model module, which does not contain any (or much) 
> implementation code:
> -model.py----------------------------------------------------------------------
> 
> import innermodel
> 
> __implementation = innermodel.Implementation()
> 
> def methodA():
>      "Example of a method interface."
>      __implementation.methodA()
> 
> def TestClass():
>      "Example of a class interface."
>      return __implementation.TestClass()
> 
> -------------------------------------------------------------------------------
> 
> and then the actual implementation code:
> -innermodel.py-----------------------------------------------------------------
> 
> class Implementation:
>      def __init__(self):
>          print "Implementation initializing..."
> 
>      def methodA(self):
>          print "Implementation methodA called."
> 
>      class TestClass:
>          def __init__(self):
>              print "TestClass initializing..."
> 
>          def methodB(self):
>              print "TestClass methodB called."
> -------------------------------------------------------------------------------
> 
> I am not sure if this is what they mean, and if so which is this - a 
> proxy for an internal class that can be changed, or does it simply 
> contain the internal workings.

Neither, really. You haven't provided the abstract base class to show 
the API, and you are not really delegating to Implementation.TestClass. 
methodA() (which is not a method) is a proxy function because it 
delegates to __implementation.
>
> It seems to provide some degree of data hiding, but perhaps too much? 

Not really, clients can still access __implementation, it's just a 
little harder.

> You cannot figure out from the model.py file what are the available 
> methods for the TestClass class.  I guess that these could be documented 
> in the TestClass docstring.

Ask yourself what is the benefit of all this?

Kent



More information about the Tutor mailing list