Why less emphasis on private data?
sturlamolden at yahoo.no
Sun Jan 7 17:47:07 CET 2007
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?
The designers of Java, C++, C#, Ada95, Delphi, etc. seem to think that
if an object's 'internal' variables or states cannot be kept private,
programmers get an irresistible temptation to mess with them in
malicious ways. But if you are that stupid, should you be programming
in any language? The most widely used language is still C, and there is
no concept of private data in C either, nor is it needed.
As mentioned in other replies, it is not rocket science to access a
class private data. In C++ you can cast to void*, in Java and C# you
can use reflection. C++ is said to be an "unsafe" language because
programmers can, using a few tricks, mess with the vtables. But how
many really do that?
In Python variables are kept in strict namespaces. You can ask the
compiler to name mangle a variable by prepending underscores. The
variable then becomes just as 'private' as a C++ private variable,
because as previously mentioned, 'private' variables in C++ can be
accessed through a cast to void*.
More information about the Python-list