next up previous contents
Next: A First Example of Up: Writing Modules Previous: Operator Overloading   Contents

Private Attributes

In many object-oriented languages, certain attributes can be declared as private, making it impossible for users of a class to directly view or modify their values. The designer of the class then provides methods to control the ways in which these attributes can be manipulated. While Python classes don't have true private attributes, if an attribute name begins with two underscores, the Python interpreter internally modifies the attribute's name, so that references to the attribute will not be resolved. (Of course anyone willing to look up the way in which the attribute name is ``mangled'' could still access it.) Through this convention, you can create attributes which will only be available within the methods you define for the class, giving you more control over the way users of your class will manipulate those attributes.



Phil Spector 2003-11-12