Why less emphasis on private data?

Stefan Schwarzer sschwarzer at sschwarzer.net
Sat Jan 6 20:37:26 EST 2007


On 2007-01-07 01:54, BJörn Lindqvist wrote:
> Google for "python for consenting adults"
>
> Or ask yourself the opposite question. Why does C++ and C# prefer more
> private data? It is given that emphasizing private data
> (encapsulation) leads to more internal complexity and more lines of
> code because you have to write getters and setters and stuff. With
> that in mind, why do you think that data encapsulation makes code less
> error prone? Can you prove it? Or do you have anecdotal evidence of
> where data encapsulation saved your ass?
>
> IMHO, that data hiding is good, is one of those ideas that have been
> repeated so much that virtually everyone thinks it is true.  But
> Python proves that it isn't necessarily so.

I think attributes (callable or not) which relate to the
abstraction of the class should be "public" (special methods
or without leading underscore). Attributes that are there for a
specific implementation of the abstraction should be "private".

The internal implementation of a class is more-often changed
in incompatible ways than the abstraction, so distiguishing
between a public and a private interface will probably save
you from reworking the clients of a class if you prefer the
public interface. It will also make the client code easier to
understand.

Admittedly, there are special cases where you want to access
private attributes, e. g. debugging; that's ok.

In summary, the distinction between public and non-public
attributes IMHO makes sense, but I don't think that the
distinction should be enforced by the language as in C++
or Java.

Stefan



More information about the Python-list mailing list