[Tutor] Re: Public, private and protected member variables.

Andrei project5 at redrival.net
Fri Oct 17 13:40:07 EDT 2003


Marc Barry wrote:
> Dear All:
> 
> I have a question regarding the existence of protected members in 
> Python. Basically, in a language like Java we have public, private and 
> protected member variables. But in Python I only know of public and 
> private. I would like to inherit from a class and be able to modify 

Only in Python private isn't *really* private. Come to think of it, other 
languages which have public/private/protected have some awkward rules too, e.g. 
classes declared in the same unit in Delphi don't behave quite as you'd expect 
them regarding these permissions.

<snip>

> The output from running this script is:
> 
> ['_Car__colour', '_Ferrari__model', '__doc__', '__init__', '__module__', 
> 'printColour']
> Traceback (most recent call last):
>  File "inheritance.py", line 19, in ?
>    a_ferrari.printColour()
>  File "inheritance.py", line 15, in printColour
>    self.__colour
> AttributeError: Ferrari instance has no attribute '_Ferrari__colour'
> 
> As you can see the __colour member variable of Car is not visible in the 
> Ferrari class since it appears that appending '__' on a variable makes 
> it private and not protected. Is there a way to make a variable 
> protected in Python?

Actually, it is visible. The name is mangled to "_Car__colour", that's all that 
"private" does: name mangling. So it should be ok if you do in printColour 
"print self._Car__colour" rather than "print self.__colour".

Note that in Python you can access the pieces of a class using its __dict__, so 
you can't do a lot about a really persistent programmer trying to access your 
private parts - in a programming sense, that is.

> If I expose a variable in Car as public ,then I will allow users of the 
> class to modify it directly. I would like to avoid this since that fixes 
> my implementation to being always having to provide a variable named 
> "colour" for example. Also, this is just a simplification of my problem 

Python doesn't enforce this kind of security. If someone *wants* to access the 
mangled name (_Car__colour), its name makes it clear it's "private" and they're 
aware of the risk that it might change. At that point they can choose to be 
responsible programmers and not use it, or use it anyway and live with the 
consequences. I like this attitude better than that in "traditional" langauges 
which enforce private/protected, where programmers then start looking for clever 
hacks to access that supposedly secret data anyway.

> as my class is much more complicated with a lot of gettable and settable 
> properties in the parent object (Which I want to inherit).

I don't know your needs, but perhaps you're trying too hard to turn Python into 
a language with static typing? When I first started in Python (which is not that 
long ago), I had a tendency to do manual typechecking along the lines of "if 
type(x)==type(y):". Turns out that's actually not necessary.

-- 
Yours,

Andrei

=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq gur 
yvfg, fb gurer'f ab arrq gb PP.





More information about the Tutor mailing list