Why does python not have a mechanism for data hiding?
apardon at forel.vub.ac.be
Mon Jun 2 11:00:08 CEST 2008
On 2008-05-24, Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
> Sh4wn <luckyluke56 at gmail.com> writes:
>> first, python is one of my fav languages, and i'll definitely keep
>> developing with it. But, there's 1 one thing what I -really- miss:
>> data hiding. I know member vars are private when you prefix them with
>> 2 underscores, but I hate prefixing my vars, I'd rather add a keyword
>> before it.
> From whom are you trying to hide your attributes?
> In Python, the philosophy "we're all consenting adults here" applies.
> You shouldn't pretend to know, at the time you write it, all the uses
> to which your code will be put. Barriers such as enforced "private"
> attributes will only cause resentment when people, despite your
> anticipations, *need* to access them and are then forced to hack their
> way around them.
I don't find this argument very compelling.
You can't anticipate all functionality people would like your function
to have. Acces to information in a (private) attribute is just one of
those possible functionallities. People will resent you if you don't
provide functionality they think fits logically in your package.
> If you want the users of your code to know that an attribute should
> not be used as a public API for the code, use the convention of naming
> the attribute with a single leading underscore. This is a string
> signal that the attribute is part of the implementation, not the
> interface. The reader is then on notice that they should not rely on
> that attribute; but they are not *prohibited* from using it if
> necessary to their ends.
But they will resent you just as much if you decide to rewrite
your module in such a way that the attribute is no longer present
or is used now in a slightly different way, so that it break code.
More information about the Python-list