Why does python not have a mechanism for data hiding?

Roy Smith roy at panix.com
Thu Jun 5 21:20:15 CEST 2008


In article 
<ecf40385-4291-42d9-b68b-6aea3477b849 at c19g2000prf.googlegroups.com>,
 "Russ P." <Russ.Paielli at gmail.com> wrote:
 
> In C++ (and
> Java?), on the other hand, the "protected" keyword *really* prevents
> the client from accessing the data or method, but it allows access to
> derived classes. The "private" keyword goes further and prevents
> access even by derived classes.

In C++, it does no such thing.  Consider this class declaration in 
MySecretClass.h:

class MySecretClass {
private:
   int foo;
};

All somebody has to do to get at the private data is:

#define private public
# include <MySecretClass.h>
#undef private

If playing preprocessor games isn't your thing, there's a whole multitude 
of other tricks you can play with pointers and typecasts that will get you 
access to foo in other ways.

But, you protest, you're not supposed to do that!  Well, of course not.  
But you're not supposed to ignore the leading underscore convention in 
Python either.  That's the nice thing about freedom of religion; you get to 
pick which particular sins you want to get freaked out about.

I'm pretty weak on Java, but my understanding is that it's in better shape 
here, since it has neither textual inclusion of header files, nor pointers.  
You might have to resort to JNI :-)



More information about the Python-list mailing list