Class design (information hiding)

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Sep 7 07:05:08 EDT 2007


Alexander Eisenhuth a écrit :
> Hi all,
> 
> I'm wodering how the information hiding in python is ment. 

Conventions...

> As I 
> understand there  doesn't exist public / protected / private  mechanism, 
> but a '_' and '__' naming convention.

Yes.

> As I figured out there is only public and private possible as speakin in 
> "C++ manner".

Nope. It's either 'interface' (no leading underscore),  'implementation' 
(single leading underscore), 'implementation with some protection 
against accidental overriding' (two leading underscores).

> Are you all happy with it.

Can't speak for others, but as far as I'm concerned, I'm perfectly happy 
with this.

> 
> class A:
>     def __init__(self):
>         self.__z = 1
>         self._z = 2
>         self.z = 3
>     def _getX(self):
>         return "X"
>     def __getY(self):
>         return "Y"
>     def doAnything(self):
>         print self.__getY()
> 
> 
> class B(A):
>     def __init__(self):
>         A.__init__(self)
>         print dir (self)
>  >>> b = B()
> ['_A__getY', '_A__z', '__doc__', '__init__', '__module__', '_getX', 
> '_z', 'doAnything', 'z']
> 
> I was a bit surprised about '_A__getY' and '_A__z'.

It's documented.

(snip)

> What is the idea behind the _ and __ naming.

cf above.

You may also want to read this - while C++ is not Java either, some 
advises may still apply:
http://dirtsimple.org/2004/12/python-is-not-java.html

HTH



More information about the Python-list mailing list