[Tutor] Public, private and protected member variables.

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Oct 17 13:22:05 EDT 2003



On Fri, 17 Oct 2003, Marc Barry wrote:

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

Hi Mark,

(Tangential note: In some programming languages all member variables are
private by default.  Objective C is one example.)


> Here is a simple example:
>
> #------------------------------------------------------------
> class Car:
> 	def __init__(self, colour):
> 		self.__colour = colour
>
>
> class Ferrari(Car):
> 	def __init__(self, model, colour):
>
> 		Car.__init__(self, colour)
> 		self.__model = model
>
> 	def printColour(self):
> 		print self.__colour
>
> a_ferrari = Ferrari("Enzo", "Red")
> print dir(a_ferrari)
> a_ferrari.printColour()


> But in Python I only know of public and private. I would like to 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 (i.e. Protected concept in Java).

Python's variable protection is handled by convention, not law.  The
Python Style essay says that if we want to specify "protected"-style
status, we can use a single underscore in the front of the name:

    http://www.python.org/doc/essays/styleguide.html

So the informal way to handle this might be something like this:

###
class Car:
    def __init__(self, colour):
        self._colour = colour

class Ferrari(Car):
    def __init__(self, model, colour):
        Car.__init__(self, colour)
        self.__model = model

    def printColour(self):
        print self._colour
###


I know this is a toy example, but it might be better to "pull up"  the
printColour() method into the parent, and avoid the visibility problems
altogether:

###
class Car:
    def __init__(self, colour):
        self.__colour = colour

    def printColour(self):
        print self.__colour


class Ferrari(Car):
    def __init__(self, model, colour):
        Car.__init__(self, colour)
        self.__model = model
###



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


If you'd like, please feel free to talk about object design on the Tutor
list.  It's one of those things I'd like to learn more about myself.
*grin*  Perhaps it might be possible to avoid exposing so many properties
by reworking the design a little?

I think we had a recent discussion about this subject, and one thing that
came up was Java's influence was making people overdesign their classes to
use setFoo/getFoo everywhere.  Folks on the list also talked a bit about
the variable visibility stuff that you're interested in:

    http://mail.python.org/pipermail/tutor/2003-October/025599.html


Good luck to you!




More information about the Tutor mailing list