[Tutor] Protected methods/variables

Alan Gauld ukc802591034 at btconnect.com
Tue Apr 4 19:07:06 CEST 2006

"Miquel Oliete" <ktalanet at yahoo.es> wrote in message 
news:4432775C.2050806 at yahoo.es...
> I have been programming in object oriented languages for several years
> and I'm learning python now.

Congratulations :-)

> I have missed protected method/variables in Python.

In what sense have you missed them? Have you been hitting
a lot of bugs because you didn't have them? OIr do you just
mean you are having withdrawal symptoms after working in
more conservative OOP languages like C++/Java etc?

> How do you declare methods/variables used only by a class
> and their derived classes?

You don't.  Protected was a scheme introduced by Bjarne
Stroustrup at the behest of Mark Linton to support Mark's
Interviews GUI library in C++ (version 1.2)  and
subsequently copied in Java. They are part of the statically
and strictly typed idioms of OOP used in those languages.

However Stroustrup writes in his book "The Design & Evolution
of C++" : "In retrospect, I think that protected is a case where
"good arguments" and fashion overcame my better judgement..."
And Linton himself withdrew support for protected members
from Interviews around the same time - they were causing bugs
and maintenance problems!

Most OOP languages and especially dynamic OOP languages
prefer the freedom of expression and flexibility that dynamic typing
(or duck typing as its sometimes called) affords.

Objective C tries to combine boith with mixed results.

But in practice I can honestly say I have never missed having
the protected keyword (I do occasionally like to use private
- for which there is a convention in Python) but never have
I missed protected.

What do you find you need it for?

Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list