On Saturday, August 17, 2013 8:26:32 AM UTC-4, Fernando Saldanha 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

Hi FS,

I'm taking the Python Cert series w/ O'Reilly School of Technology, which I 
recommend if you've got a good handle on OO programming.  In any event, 
according to what I've learned, "encapsulation is the idea that the only way to 
access or change the data inside an object is by calling its methods. This idea 
has never really gained much ground in the Python world, and it is normally 
considered acceptable to both read and set an object's attributes from anywhere 
in a program."  Not being an expert OO programmer, I take this at face value.

There are ways to protect class attributes from having their values reset from 
outside. That is, they can be made "internal use only" and an AttributeError 
raised when someone tries to change the attribute(s).  This involves 
__setattr__  and checking if the key of the attribute is in a/the list of 
attributes you've chose to protect.  If so, raise AttributeError.  Hope that 
helps in some small measure.

In the interest of full disclosure, answering a Python question is part of my 
homework for the O'Reilly Python 4 class I'm taking.  Good luck! 

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to