On Sat, Aug 17, 2013 at 1:26 PM, <fsald...@gmail.com> wrote: > 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?
Very simply: You accept it. Let them! It's their responsibility. Python scripts are generally assumed to start at the beginning, go on till they reach the end, then stop (like the White King's advice to Alice). The author of the application is assumed to be in command of everything. If s/he chooses to monkey-patch something, so be it. If that monkey-patch breaks in the next version, it's the app author's problem. As a module or class author, you just need to make sure you don't make crazy changes to the environment, and all will be well. If you have invariants that you want to maintain, you can simply document the one official way to mutate your objects ("use the .foo() method, don't tinker with the members"), and most people will respect that. But most of the time, that's not even an issue - all you have to do is tell yourself "It's fine for them to change stuff", and (a) you save the hassle of preventing them, and (b) you save the hassle of writing tons of boilerplate to grant specific access. class Point def __init__(self,x,y): self.x,self.y=x,y def distance(self,other): return math.sqrt((self.x-other.x)**2+(self.y-other.y)**2) foo = Point(0,0) while True: foo.x+=deltax; foo.y+=deltay if foo.distance(bar)>50: break Easy! No getter/setter needed. ChrisA -- http://mail.python.org/mailman/listinfo/python-list