Encapsulation unpythonic?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Aug 17 12:17:40 EDT 2013


On Sat, 17 Aug 2013 05:26:32 -0700, fsaldan1 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. 

Utter nonsense. Whoever told you this doesn't understand what 
encapsulation is.

Python encapsulates related code into objects. It encapsulates related 
objects into modules. It encapsulates related modules into packages.


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

That's *data hiding*, not encapsulation.

Very few languages -- possibly none at all -- can hide data from a 
sufficiently motivated programmer. Python doesn't even try. "We're all 
adults here" is the philosophy, and data hiding is by convention, not 
enforced by the compiler.

Single leading underscores are "private". Don't touch them. If you do, 
and code breaks, nobody will give you sympathy. You have nobody to blame 
but yourself.

Double leading underscores are "private", and also have name-mangling to 
try to avoid certain inheritance-related issues. In general, it's more of 
a nuisance than anything else, so most people don't bother. Consider 
double underscore __names to be for advanced OOP usage, 98% of the time a 
single underscore is enough.

Double leading and trailing __names__ are reserved for Python. They're 
not necessarily private, but if you're calling them directly, you're 
probably doing something wrong. Again, consider them to be advanced usage.


> Questions:
> 
> 1) Is there a good text where I can read about the language philosophy?
> What practices are "pythonic" or "unpythonic"?

Good question!

Start at the interactive interpreter:

import this


This is a reasonable description of what it means to be Pythonic:

http://blog.startifact.com/posts/older/what-is-pythonic.html


This is a good pair of resources, comparing the Java and Python 
philosophies, and the strengths of each:

http://dirtsimple.org/2004/12/python-is-not-java.html

http://dirtsimple.org/2004/12/java-is-not-python-either.html

Also, it helps to understand that Python is named after Monty Python, not 
the snake. It's not necessary to like anarchic British humour, but it 
helps to get some of the references. We'll talk about "spam, ham, eggs" 
rather than "foo, bar, baz", and the Cheeseshop, and the Spanish 
Inquisition, and Norwegian Blue parrots.

But ultimately, writing Pythonic code doesn't come from reading a list of 
rules. It comes from becoming comfortable with the language, from 
understanding its strengths and weaknesses, from reading lots of people's 
code, and writing lots of code, and learning the idioms.


> 2) If it is in fact true that encapsulation is rarely used, 

Not true. It is true that data hiding is really used though, at least in 
pure-Python code, except by convention.

(C extensions are much more strict about data hiding, since you can crash 
the compiler if you muck about with C-level internals. Exceptions are a 
good thing. Segfaults are not.)


> how do I
> deal with the fact that other programmers can easily alter the values of
> members of my classes?

Embrace it! That's a good thing!

In Java or C++ or other languages, other programmers are going to alter 
your classes' members anyway. The only difference is that they will spend 
hours or days fighting the compiler in order to do so, and eventually end 
up with horrible, fragile, non-portable code.

Besides, while it's nearly always a Bad Thing to mess with private 
attributes, sometimes it is a really, really Useful Thing to *inspect* 
private attributes, for debugging. Python makes that easy.

Treat other programmers as adults, and they in turn will treat you the 
same way. If they insist on messing with your private single-underscore 
_attributes, you can't stop them, but that's okay, you don't have to be 
sympathetic when they shoot their foot off. Just slap them with a large 
halibut[1] and laugh.





[1] Another Monty Python reference.


-- 
Steven



More information about the Python-list mailing list