Why does python not have a mechanism for data hiding?

Patrick Mullen saluk64007 at gmail.com
Mon May 26 00:43:28 EDT 2008


There are certainly situations where data hiding is useful and perhaps a
wanted feature, but there has to be at least one place where I can use what
I want when I want to with some high and mighty designer telling me what I
should and shouldn't access.  One of the reasons they have data hiding in
something like Java, is because often you will use get/set functions that
actually do more than tell you the value of an attribute.  Python uses
properties or overloaded functions for this same mechanism.

Because of this design, you are less likely to have to change an "internal"
attribute that is relied on in user code.  Instead of telling the user that
now they have to use a different function to get a value, or changing the
name of an attribute and confusing users, you can always proxy things making
the interface stay backwards compatible.

Obviously this doesn't suit all situations, every language consideration is
going to be good in some cases and bad in others.  Otherwise it wouldn't be
a choice.  Python has made an actual choice to NOT include this feature.
Things like properties or underscore conventions etc are alternative
solutions for things that data hiding is supposed to solve.  Obviously it is
open source, so if enough people complain loud enough it could change, but
this is something that has stuck for a long time, and also a reason many
long time python developers are still using it.  Just as open source
software allows users to do new things with old programmers or revive
projects which are dead; python allows libraries to be extended easily
protecting users from designers who think they know what we want to be able
to do with their code.

But yeah, in a situation where you want the freedom to change around
internal apis, and your customers won't follow your guidelines, I feel for
you :)  I think mostly we all fear that if true data hiding were added (a la
java), that it would be abused and not limited to situations you describe.
Which is not necesarily a valid assumption, since python has been around a
long time with the philosophy it has (it IS a philosophy and guides many
other python features or lack of features), and library writers may continue
to develop the way they have previously.  All of us who complain when anyone
mentions data hiding would not use these features, and I'm fairly sure the
system libraries would remain open.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080525/0201169f/attachment-0001.html>


More information about the Python-list mailing list