Why less emphasis on private data?
macdev at artins.org
Sun Jan 7 12:31:08 CET 2007
In article <1168128425.058049.221320 at v33g2000cwv.googlegroups.com>,
"time.swift at gmail.com" <time.swift at gmail.com> wrote:
> Coming from a C++ / C# background, the lack of emphasis on private data
> seems weird to me. I've often found wrapping private data useful to
> prevent bugs and enforce error checking..
> It appears to me (perhaps wrongly) that Python prefers to leave class
> data public. What is the logic behind that choice?
> Thanks any insight.
One thing that the other posters didn't mention is that if you access data
members of a class in C++ you end up with a very tight coupling with that class.
If the class later changes so that the data is no longer part of the public
interface, then every user of the class has to change the code and recompile.
In Python, on the other hand, if I have a piece of public data that I later
decide to replace with an accessor method, I can do that without changing any of
the code that uses the class.
So, insistence on private data in C++ is a good thing because it reduces the
level of coupling between a class and its clients. In Python, this is not an
issue, because the same loose coupling can be obtained with data as well as
accessor methods, and therefore public data is used when possible and private
data when necessary.
If this message helped you, consider buying an item
from my wish list: <http://artins.org/ben/wishlist>
I changed my name: <http://periodic-kingdom.org/People/NameChange.php>
More information about the Python-list