Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'scripts': 0.03; '(b)': 0.07; 'alter': 0.09; 'assumed': 0.09; 'method,': 0.09; 'python': 0.11; 'def': 0.12; 'changes': 0.15; '"it\'s': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'rarely': 0.16; 's/he': 0.16; 'true:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'app': 0.19; 'module': 0.19; 'command': 0.22; 'aug': 0.22; '(a)': 0.24; 'fine': 0.24; 'values': 0.27; 'header :In-Reply-To:1': 0.27; 'point': 0.28; 'generally': 0.29; '(like': 0.30; 'needed.': 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'author,': 0.31; 'end,': 0.31; 'class': 0.32; 'programmers': 0.33; 'used,': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'advice': 0.35; 'problem.': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'crazy': 0.36; 'next': 0.36; 'subject:?': 0.36; 'application': 0.37; 'easily': 0.37; 'version,': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'fact': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'most': 0.60; 'tell': 0.60; 'break': 0.61; 'till': 0.61; 'simply': 0.61; 'save': 0.62; 'reach': 0.63; 'skip:m 50': 0.68; 'respect': 0.70; 'yourself': 0.78; 'everything.': 0.84; 'hassle': 0.84; 'preventing': 0.84; 'subject:skip:E 10': 0.95; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=j047if2ZoPxwt854kWqTBT27PXrb4xXAmgobPdIqZys=; b=Mfp/bIrJQJdz7yGELFm/EhoXRQE0RstejdC9Ax+AiXMGWAUnckJoZJSaPTaqZ+N6An MDdix9Sxr/JEt83NlqPspgvQ+Ie8sorRj6VNttO2IfuYirp77uYk84NMw8ZnMC+UD7IJ 4dMEn4VcsJDahLx0/QDTssaCNCWx9ik7IEczata/7q5pLNSClgCIlmZiVQ/0XpBVJSsT qFgvreJqD2nRmOhaSyXcDgn3ukXgwgbUj1QNWTcFYsQNtXRIdW/ElwU9Ke+5OPDggwkp lHZH30HQw+Iut9/h4onzFDtGGnNcnQXaINtJNmJ4VevIB9X8n3g3+jXDDCKupjWUE/XI W9ow== MIME-Version: 1.0 X-Received: by 10.220.145.132 with SMTP id d4mr6945746vcv.9.1376754606415; Sat, 17 Aug 2013 08:50:06 -0700 (PDT) In-Reply-To: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> References: <8255dfbd-a2a1-4ab7-b900-ee19faa459f2@googlegroups.com> Date: Sat, 17 Aug 2013 16:50:06 +0100 Subject: Re: Encapsulation unpythonic? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376754615 news.xs4all.nl 15984 [2001:888:2000:d::a6]:33091 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52632 On Sat, Aug 17, 2013 at 1:26 PM, 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