[Tutor] Data hiding in Python.

Alan Gauld alan.gauld at btinternet.com
Wed Dec 20 02:04:40 CET 2006


"Alan Gauld" <alan.gauld at btinternet.com> wrote 

> public/private is overkill. I actually like the Delphi 2 model
> (they have since added protected etc) was a good compromise
> where implementation section(*) attributes 

Oops!
I meant to add a footnote here that explained that Delphi modules 
comprise two sections, an interface and an implementation. Only 
functions declared in the interface can be used by clients of the 
module. I often think that this scheme would be a useful addition 
to Python's module structure. It would look something like this:

#### module foo ####
interface:

def myfunc(p1,p2)

def anotherFunc(p3)

class bar:
   def __init__(...)
   def f(self)
   def g(self,x)

mylist = []    # can be used by clients

implementation:

secretVar = {}   # hidden from clients

def myfunc(p1,p2):
     #code here can use secretVar

def anotherFunc(p3):
     # code here

class bar:
   def __init__(self):
      self.x = 42
   # etc...
#########################

Its fairly easy to understand and provides a good mix of 
access control and readability without tying the programmers 
hands.

It also means you can very easily see all that a module has 
to offer by just reading the top (relatively) few lines of code.
It thus gives the advantages of C header files without the 
problems of managing/synchronising two files per module.

All IMHO of course... :-)

Alan G.





More information about the Tutor mailing list