Section 13.14. Privacy


13.14. Privacy

Attributes in Python are, by default, "public" all the time, accessible by both code within the module and modules that import the module containing the class.

Many OO languages provide some level of privacy for the data and provide only accessor functions to provide access to the values. This is known as implementation hiding and is a key component to the encapsulation of the object. Most OO languages provide "access specifiers" to restrict access to member functions.

Double Underscore ( __ )

Python provides an elementary form of privacy for class elements (attributes or methods). Attributes that begin with a double underscore (__) are mangled during runtime so direct access is thwarted. In actuality, the name is prepended with an underscore followed by the class name. For example, let us take the self.__num attribute found in Example 13.6 (numstr.py). After the mangling process, the identifier used to access that data value is now self._NumStr__num. Adding the class name to the newly mangled result will prevent it from clashing with the same name in either ancestor or descendant classes.

Although this provides some level of privacy, the algorithm is also in the public domain and can be defeated easily. It is more of a protective mechanism for importing modules that do not have direct access to the source code or for other code within the same module.

The other purpose of this type of name-mangling is to protect __XXX variables from conflicting with derived class namespaces. If you have an __XXX attribute in a class, it will not be overridden by a child class's ___XXX attribute. (Recall that if a parent has just an XXX attribute and a child defines one, then the child's XXX overrides the parents, and the reason why you have to do PARENT.XXX to call the base class method of the same name.) By using __XXX, the code for the child class can safely use __XXX without worrying that it will use or affect __XXX in the parent.

Single Underscore ( _ )

As we discovered in Chapter 12, simple module-level privacy is provided by using a single underscore ( _ ) character prefixing an attribute name. This prevents a module attribute from being imported with "from mymodule import *". This is strictly scope-based, so it will work with functions too.

With Python's new-style classes introduced in 2.2, a whole new set of features was added to give programmers a significant amount of control over how much protection is offered class and instance attributes. Although Python does not have syntax built into the language that has the flavors of private, protected, friend, or protected friend, you can customize access in the exact way that fits your needs. We cannot cover all of those possibilities but will give you an idea of the new-style we attribute access later in this chapter.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net