[Python-Dev] PEP 8 updates/clarifications

Jason Orendorff jason.orendorff at gmail.com
Wed Dec 14 05:18:24 CET 2005


Barry Warsaw wrote:
>       - If your class is intended to be subclassed, and you have attributes
>         that you do not want subclasses to use, consider naming them with
>         double leading underscores and no trailing underscores.  This invokes
>         Python's name mangling algorithm, where the name of the class is
>         mangled into the attribute name.  This helps avoid attribute name
>         collisions should subclasses inadvertently contain attributes with the
>         same name.
>
>         Note 1: Note that only the simple class name is used in the mangled
>         name, so if a subclass chooses both the same class name and attribute
>         name, you can still get name collisions.
>
>         Note 2: Name mangling can make certain uses, such as debugging, less
>         convenient.  However the name mangling algorithm is well documented
>         and easy to perform manually.

Hmm.  How about just:  "Put two leading underscores on an attribute's
name to strongly discourage code outside the class from accessing it."

-j


More information about the Python-Dev mailing list