[Poll] Private variables

Christian Tanzer tanzer at swing.co.at
Thu Sep 13 07:40:08 CEST 2001


"Alex Martelli" <aleax at aleax.it> wrote:

> My personal opinion is that __beep style names serve to avoid
> accidental clashes of identifiers between base and derived
> classes -- you can use that style of identifiers without any
> worries about some ancestor or descendant class duplicating
> it by accident, and that's the only feature that makes them
> really useful.

Unless you use names like these in your hierarchy:

    Python 2.1 (#1, May  2 2001, 18:27:26) 
    [GCC 2.7.2.1] on linux2
    Type "copyright", "credits" or "license" for more information.
    >>> class _A :
    ...   __beep = 1
    ... 
    >>> class A(_A) :
    ...   __beep = 2
    ... 
    >>> class B(A) :
    ...   __beep = 3
    ... 
    >>> dir (_A)
    ['_A__beep', '__doc__', '__module__']
    >>> dir(A)
    ['_A__beep', '__doc__', '__module__']
    >>> dir(B)
    ['_B__beep', '__doc__', '__module__']
    >>> 

The name mangling of `__bar` names misbehaves if the class name starts
with a single underscore. To be able to use `__bar` names safely one
has to avoid `_Foo` class names. 

This is the only example of accidental feature interaction in Python
that I'm aware of. [It once cost me the better part of a day to figure
out why my program suddenly went catatonic.]

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list