Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52632

Re: Encapsulation unpythonic?

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 <rosuav@gmail.com>
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 <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16.1376754615.23369.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, Aug 17, 2013 at 1:26 PM,  <fsaldan1@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Encapsulation unpythonic? fsaldan1@gmail.com - 2013-08-17 05:26 -0700
  Re: Encapsulation unpythonic? Skip Montanaro <skip@pobox.com> - 2013-08-17 07:54 -0500
  Re: Encapsulation unpythonic? David <bouncingcats@gmail.com> - 2013-08-17 23:05 +1000
  Re: Encapsulation unpythonic? Chris Angelico <rosuav@gmail.com> - 2013-08-17 16:50 +0100
  Re: Encapsulation unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-17 16:17 +0000
    Re: Encapsulation unpythonic? Joshua Landau <joshua@landau.ws> - 2013-08-18 18:15 +0100
      Re: Encapsulation unpythonic? Steven D'Aprano <steve@pearwood.info> - 2013-08-19 07:05 +0000
        Re: Encapsulation unpythonic? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-19 18:57 -0400
        Re: Encapsulation unpythonic? random832@fastmail.us - 2013-08-21 12:52 -0400
          Re: Encapsulation unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-22 02:06 +0000
        Re: Encapsulation unpythonic? Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-21 16:02 -0600
  Re: Encapsulation unpythonic? Gary Herron <gary.herron@islandtraining.com> - 2013-08-17 13:16 -0700
  Re: Encapsulation unpythonic? Terry Reedy <tjreedy@udel.edu> - 2013-08-17 17:22 -0400
  Re: Encapsulation unpythonic? Chris Angelico <rosuav@gmail.com> - 2013-08-18 01:59 +0100
  Re: Encapsulation unpythonic? chaz2cry@gmail.com - 2013-08-22 19:12 -0700
  Re: Encapsulation unpythonic? Fabrice Pombet <fp2161@gmail.com> - 2013-08-30 10:43 -0700
    Re: Encapsulation unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 02:35 +0000
      Re: Encapsulation unpythonic? Fabrice Pombet <fp2161@gmail.com> - 2013-08-30 23:07 -0700
        Re: Encapsulation unpythonic? Marco Buttu <marco.buttu@gmail.com> - 2013-08-31 08:49 +0200
        Re: Encapsulation unpythonic? Gary Herron <gherron@digipen.edu> - 2013-08-31 00:03 -0700
          Re: Encapsulation unpythonic? Fabrice Pombet <fp2161@gmail.com> - 2013-08-31 00:42 -0700
            Re: Encapsulation unpythonic? Fabrice Pombet <fp2161@gmail.com> - 2013-08-31 01:00 -0700
        Re: Encapsulation unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 11:46 +0000
          Re: Encapsulation unpythonic? Ned Batchelder <ned@nedbatchelder.com> - 2013-08-31 08:41 -0400
            Re: Encapsulation unpythonic? Fabrice Pombet <fp2161@gmail.com> - 2013-08-31 05:49 -0700
          Re: Encapsulation unpythonic? Fabrice Pombet <fp2161@gmail.com> - 2013-08-31 05:57 -0700
            Re: Encapsulation unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-01 08:10 +0000
              Re: Encapsulation unpythonic? Chris Angelico <rosuav@gmail.com> - 2013-09-01 18:21 +1000
                Re: Encapsulation unpythonic? Fabrice Pombet <fp2161@gmail.com> - 2013-09-01 03:09 -0700
                Re: Encapsulation unpythonic? Ethan Furman <ethan@stoneleaf.us> - 2013-09-01 11:42 -0700
                Re: Encapsulation unpythonic? Roy Smith <roy@panix.com> - 2013-09-01 15:13 -0400
                Re: Encapsulation unpythonic? Ethan Furman <ethan@stoneleaf.us> - 2013-09-01 13:33 -0700
                Re: Encapsulation unpythonic? Tim Delaney <timothy.c.delaney@gmail.com> - 2013-09-02 07:54 +1000
                Re: Encapsulation unpythonic? Roy Smith <roy@panix.com> - 2013-09-01 18:02 -0400
                Re: Encapsulation unpythonic? Ethan Furman <ethan@stoneleaf.us> - 2013-09-01 17:24 -0700
                Re: Encapsulation unpythonic? Roy Smith <roy@panix.com> - 2013-09-01 20:59 -0400
                Re: Encapsulation unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-02 02:57 +0000
                Re: Encapsulation unpythonic? Ethan Furman <ethan@stoneleaf.us> - 2013-09-01 21:15 -0700
                Re: Encapsulation unpythonic? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-02 01:57 +0000
              Re: Encapsulation unpythonic? Ben Finney <ben+python@benfinney.id.au> - 2013-09-02 09:32 +1000
                Re: Encapsulation unpythonic? Roy Smith <roy@panix.com> - 2013-09-01 20:52 -0400
          Re: Encapsulation unpythonic? "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2013-09-03 00:29 +0100
            Re: Encapsulation unpythonic? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-09-03 22:41 -0400
        Re: Encapsulation unpythonic? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-31 13:31 -0400
        Re: Encapsulation unpythonic? Tim Delaney <timothy.c.delaney@gmail.com> - 2013-09-01 03:47 +1000
    Re: Encapsulation unpythonic? 88888 Dihedral <dihedral88888@gmail.com> - 2013-09-02 14:43 -0700

csiph-web