Privacy and Inheritance

Delaney, Timothy tdelaney at avaya.com
Wed Sep 4 18:56:13 EDT 2002


> From: Eric Brunel [mailto:eric.brunel at pragmadev.com]
> 
> In languages that have actual "private", "protected" and "public" 
> attributes, private attributes are not inherited. They are 
> visible only in 
> the class that define them, not outside it and not in its 
> sub-classes. What 
> you want is a protected attribute, that is visible in the 
> defining class 
> and in its sub-classes.
> 
> Unfortunately, Python doesn't know anything about protected 
> attributes. 
> AFAIK, there's no simple way to do what you want: in Python, 
> attributes are 
> either private or public; there's nothing in between...

Actually, there is.

Anything beginning with a leading underscore is not imported via a 'from
module import *' (not that anyone should in general use this ;) Thus
anything with a leading underscore is something other than completely
"public".

There is a (strong) convention that anything beginning with a *single*
leading underscore is "protected". No name mangling is performed, but the
name is flagged as "special".

class StandardNormal:

    def letEventZScore(self, eventZScore):
        self._eventZScore = float(eventZScore)

class DerivativeNormal(StandardNormal):

    def getCentral(self):
        if self._eventZScore < 0:

Tim Delaney




More information about the Python-list mailing list