About classes and OOP in Python

Ben Cartwright bencvt at gmail.com
Tue Apr 11 22:05:45 EDT 2006


Michele Simionato wrote:
> Roy Smith wrote:
> <snip>
> > That being said, you can indeed have private data in Python.  Just prefix
> > your variable names with two underscores (i.e. __foo), and they effectively
> > become private.  Yes, you can bypass this if you really want to, but then
> > again, you can bypass private in C++ too.
>
> Wrong, _foo is a *private* name (in the sense "don't touch me!"), __foo
> on the contrary is a *protected* name ("touch me, touch me, don't worry
> I am protected against inheritance!").
> This is a common misconception, I made the error myself in the past.

Sure, if you only consider "private" and "protected" as they're defined
in a dictionary.  But then you'd be ignoring the meanings of the
public/private/protected keywords in virtually every language that has
them.  http://www.google.com/search?q=public+private+protected

Python doesn't have these keywords, but most Python programmers are at
least somewhat familiar with a language that does use them.  For the
sake of clarity:
  __foo ~= private = used internally by base class only
  _foo ~= protected = used internally by base and derived classes

The Python docs use the above definitions.

--Ben




More information about the Python-list mailing list