Why less emphasis on private data?

Paul Rubin http
Sat Jan 6 20:38:06 EST 2007


"BJörn Lindqvist" <bjourne at gmail.com> writes:
> 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.

You can have public variables in Java if you choose to.  Writing
private variables with public setters and getters is just a style choice.

> Or do you have anecdotal evidence of where data encapsulation saved
> your ass?

There are certainly applications that can't live without it, like
browser applets.

As for it saving my ass, there's no way to know, it's like asking
whether garbage collection has saved my ass.  Yes I've had plenty of
pointer related bugs in C programs that don't happen in GC'd
languages, so GC in that sense saves my ass all the time.  I've also
had bugs in Python programs that would have been prevented by better
use of encapsulation (including in the stdlib).  Python certainly
makes you spend more of your attention worrying about possible
attribute name collisions between classes and their superclasses.  And
Python's name mangling scheme is leaky and bug-prone if you ever
re-use class names.  Overall, I think Python would gain from having
better support for encapsulation and C++-like casting between class
instances.



More information about the Python-list mailing list