Privacy and Inheritance

Eric Brunel eric.brunel at pragmadev.com
Thu Sep 5 12:11:52 CEST 2002


Delaney, Timothy wrote:

>> 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".

I just meant that Python has no way of enforcing "protectedness", as 
leading double underscores (kind of) enforce privateness. As you say, using 
a single leading underscore in protected attribute names is merely a 
convention, even if it's a strong one: Python won't prevent any use of the 
attribute, neither from sub-classes, nor from the "outside world". Anybody 
not knowing the convention can use these attributes in any way without any 
problem.
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list