Encapsulation unpythonic?

Gary Herron gary.herron at islandtraining.com
Sat Aug 17 16:16:32 EDT 2013


On 08/17/2013 05:26 AM, fsaldan1 at gmail.com wrote:
> I am new to Python, with experience in Java, C++ and R.
>
> As I understand encapsulation is not a big thing in the Python world. I read that you can put two underscores before the name of a variable within a class declaration but in the many examples of code I looked at this is not widely used. I also read that encapsulation is "unpythonic."
>
> Questions:
>
> 1) Is there a good text where I can read about the language philosophy? What practices are "pythonic" or "unpythonic"?
>
> 2) If it is in fact true that encapsulation is rarely used, how do I deal with the fact that other programmers can easily alter the values of members of my classes?
>
> Thanks for any insights.
>
> FS


You are confusing encapsulation with data hiding!

Encapsulation is very much a part of Python.  Every class, module, 
indeed every object, encapsulates some kind of behavior.

However, *hiding* the members of a class is not considered Pythonic.  
There is no private/public as in C++, however, there are way to achieve 
that effect.

Gary Herron




More information about the Python-list mailing list