Newbie Q: Class Privacy (or lack of)

Ben Sizer kylotan at gmail.com
Fri Jul 28 06:09:22 EDT 2006


Steve Jobless wrote:
> Unfortunately, I don't see a way
> to separate interface from implementation in Python. So, it may not make
> much difference whether you subclass it or hack it from the outside.

Documentation. (And name-mangling, underscore prepending, all the stuff
the others said.) Whenever I use a 3rd party library, I consult the
docs to see how it works and which functions do what. The interface is
what is published and the implementation is what you find if you dig
around in the module. Yes, there's little in the way of run-time
enforcement of this divide. But that's because Python gives you other
benefits that come from run-time assignment and re-assignment of
attributes. It's a common and powerful idiom that you find in several
languages, especially that are wholly or partially prototype-based,
such as Javascript.

> Unfortunately, it seems to be taking unnecessary risk. I was
> hoping someone shows me a compelling reason to sacrifice the safety, but
> I don't hear much.

I think the main issue is that tracking whether an attribute is public
or private and whether an accessing scope is allowed to access it would
impose a burden on the language developers, extra typing for users, and
a run-time performance penalty, in the pursuit of some degree of
'safety' that isn't universally agreed to be useful. With this in mind,
the name-mangling approach is a very reasonable middle ground. Every
language has a philosophy and Python's differs from that of (for
example) Java. It may not be suitable for large teams of mediocre
programmers who require the compiler to keep them in line, but might
produce better results than Java in other circumstances.

-- 
Ben Sizer




More information about the Python-list mailing list