[Tutor] Public, private and protected member variables.

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Oct 17 18:45:43 EDT 2003


> I have a question regarding the existence of protected members in
Python.

We had a longish thread on variable scoping in Python just recently,
try searching the archives.

> Basically, in a language like Java we have public, private and
protected
> member variables.

Indeed, Java copied the concept from C++ which introduced it
in version 2, partly to help manage complexity in the face of
multiple inheritance(also introduced in version 2).

> But in Python I only know of public and private.

And even private can be bypassed fairly easily (as it can
in C++!) and in practice public variables are the norm.

> inherit from a class and be able to modify parent member variables,
> but I would not like any class that doesn't inherit from the parent
> to be able to modify the member variables

Why? Apart from paranoia born from exposure to languages
like C++ and Java(which as I say just played copycat?)

OO programmers have been using inheritance for many years
without protected scopes (and originally often without
private either!) and it very rarely causes problems.
If you really do need to hide the variables completely
then simply make them private and then provide accessor
functions(yuk!) or make them properties(slightly better).
The accessor can if you really want to, check the immediate
parent of the calling class...if you really want to...

But do you habitually find yourself breaking your own design
by trying to directly access internal state? Or do you really
distrust your fellow programmers that much? And what about
the occasions when internal access is actually needed by
a non child class? C++ provides the friend construct,
should Python do likewise?

Its easier to just go with the flow when using a new language.
One of the reasons that C++ and Java are so verbose and
difficult to use is that they introduce all manner of
artificial restictions which programmers have to work around.
(And I say that as someone who used C++ almost exclusively
from 1988 through to 1996 - then I found Delphi, hooray!
Then I found Python, double hooray! And I haven't written
any serious C++ since 1999. And I studiously avoid Java as
much as possible) Python tries to make the programmers life
easy at the expense of expecting a little courtesy to be
exercised.

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

If that's your concern you could override setattr and getattr
to prevent access to anything that wasn't a method. That
way all attributes are private. Then you have to provide
accessor functions - is that really any better?

Alternatively, in your documentation just tell users not
to access the attributes directly. And that doing so may result
in undefined behaviour!

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

Yep, that's why lots of gettable/settable properties are a pain.
See the previous thread... Sorry but the gettable/settable stuff
is one of my hot buttons :-(

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list