thmpsn....@gmail.com wrote >> This allows people to meddle with internals, at their own risk, >> if it ends up being absolutely necessary. > > If it ends up being necessary, the class's design is flawed. (Though > in this case, the flaw is easily solved by simply providing a getter.)
No the class design is not necessarily flawed. Sometimes it is desirable to modify a class's behavior at a lower level. In Java this isn't possible directly and so it has led to an entire class of libraries that provide means of doing this. For more information, look up "Aspect-oriented Programming" on wikipedia. Since most aspect-oriented programming examples I've seen are in Java I never understood much about it ("cross-cutting concerns" is a pretty meaningless explanation) until I realized that metaprogramming in python, monkey-patching classes, and dynamically adding attributes to an existing class are all forms of aspect-oriented programming. And it turns out to be quite useful for some things. One area where aspect-oriented programming is useful is in libraries that add security layers to objects and methods. Rather than having to make code directly aware of the security layer (which could take different forms), we can instead reach into the classes and, for example, wrap certain methods in a function that enforces security. If we do it in the class directly, then even code that descends from this class will transparently "inherit" all of the newly added security layer code that was never there when the code was first written. By security layer I'm talking more about code that enforces certain user authentication before use (as in web programming) rather than "secure" code. -- http://mail.python.org/mailman/listinfo/python-list