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


Groups > comp.lang.python > #104528

Re: Encapsulation in Python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Encapsulation in Python
Date Thu, 10 Mar 2016 08:27:09 -0700
Lines 54
Message-ID <mailman.139.1457623672.15725.python-list@python.org> (permalink)
References <56E17985.7060002@benmezger.nl>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 5kL3L+Ledq6/4tYw/BccPQj6mwDrrS1wWcMNALwJ/LSg==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:Python': 0.05; 'attributes': 0.07; 'attribute.': 0.09; 'convention,': 0.09; 'logic': 0.09; 'mess': 0.09; 'underscore': 0.09; 'vast': 0.09; 'python': 0.10; 'python.': 0.11; ':-)': 0.12; 'java,': 0.15; 'thu,': 0.15; '(data': 0.16; '2016': 0.16; 'attribute,': 0.16; 'attribute;': 0.16; 'benefit.': 0.16; 'foo(object):': 0.16; 'java.': 0.16; 'mine.': 0.16; 'naming': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'so;': 0.16; 'underscore.': 0.16; 'undesirable': 0.16; 'wrote:': 0.16; 'later': 0.16; "wouldn't": 0.16; 'attribute': 0.18; 'all,': 0.20; 'prevent': 0.20; 'meant': 0.22; 'bar.': 0.22; 'either.': 0.22; 'oriented': 0.22; 'am,': 0.23; 'code.': 0.23; 'header:In-Reply- To:1': 0.24; "i've": 0.25; 'external': 0.27; 'message- id:@mail.gmail.com': 0.27; 'looks': 0.29; 'creating': 0.30; 'code': 0.30; 'normally': 0.30; 'getting': 0.33; 'point': 0.33; 'class': 0.33; 'usually': 0.33; 'belong': 0.33; 'foo': 0.33; 'int': 0.33; 'lets': 0.33; "i'll": 0.33; 'except': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'c++': 0.35; 'exist': 0.35; 'replace': 0.35; 'something': 0.35; "isn't": 0.35; 'community': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'setting': 0.37; 'received:209.85.213': 0.37; 'doing': 0.38; 'itself': 0.38; 'received:209': 0.38; 'means': 0.39; 'data': 0.39; 'does': 0.39; 'easily': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'still': 0.40; 'some': 0.40; 'field': 0.60; 'your': 0.60; 'leading': 0.61; 'real': 0.62; 'making': 0.62; 'world': 0.64; 'mar': 0.65; 'future.': 0.67; 'designers': 0.72; 'special': 0.73; 'get/set': 0.84; 'messed': 0.84; 'studying': 0.84; 'to:name:python': 0.84; 'subject:skip:E 10': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=HKy7n89Rt5uk5+deWySMr4O9AS4LyW9Vk/cvc/JK9x0=; b=qzHwZzlb9ipEx7iN9u2ORI18Dozqj/4nE056IGH/qeL4rReDdIeThXmAxJ9Lj1uJ+D XRNxFTCgJ2l0RUrNcjNjkdlr891CTTY1qJum212gy9ToiwjJToHNgkJBT/g3GvzHf+Y7 L6DKY8Dxgc/5PFq9KSLFr7ZeFbRmKPXIuXTkqcof/Ha0U/7a7Y6KOlslLtLBMu2KndHR HTwQ+SSWrnFvSOjpGiyuciJYOnZjqc7XB43g1zK3d3h7zaYF4vA+neK7XrBi2wWAfer+ 5DJ/shZms6jjI7qqHGAq0AnysHfpPRX33fmOtKR/kSVo6ouP05kXX5PonweVG69pr0wf X8yw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=HKy7n89Rt5uk5+deWySMr4O9AS4LyW9Vk/cvc/JK9x0=; b=kAwCXRTFnYn4F2QkPT4gaUKdoa+f1UihYtKjpgG0X/noyHeCcgkQ6CG/mvaNWWNrHl ohOqJoGFifXEpAPkt3mq8xcS/pDsa/IGN7zxnJyYnHZGTlmid7qe/Ao5EZapmzjONRCk iEwRzbfRA+e9z1QYZ7kmYQYoVEJD3XodaYu2FNfwVht2dC9yAw74Ee7tRql5mygX6GjS BKs0wiafZMS8UuBPJQxYWk6A5ziHc1SN1/cwXrS6+EO3lsCUhvQFuLGkzqQz3uTbMkh/ qCjfAP2Gw5rJy39eG1wfOgfkgJGuohS+o+qbNITqy47GR+6ALbLU+8ZX6MUoD9X5ODtR GIUA==
X-Gm-Message-State AD7BkJKNECGf71F0tKi2/+2/7COOXcZRked7ZjZumhl27yR/IsNdPPqeAXR7+CKPJAv0E1FcL1Dq0biSlz2h2w==
X-Received by 10.50.176.138 with SMTP id ci10mr4398178igc.93.1457623669456; Thu, 10 Mar 2016 07:27:49 -0800 (PST)
In-Reply-To <56E17985.7060002@benmezger.nl>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:104528

Show key headers only | View raw


On Thu, Mar 10, 2016 at 6:41 AM, Ben Mezger <me@benmezger.nl> wrote:
> Hi all,
>
> I've been studying Object Oriented Theory using Java. Theoretically, all
> attributes should be private, meaning no one except the methods itself
> can access the attribute;
>
> public class Foo {
>     private int bar;
>     ...

Encapsulation in Python is based on trust rather than the
authoritarian style of C++ / Java. The maxim in the Python community
is that "we're all consenting adults". If I don't intend an attribute
to be messed with, then I'll mark it with a leading underscore. If you
mess with it anyway, and later your code breaks as a result of it,
that's your problem, not mine. :-)

> Normally in Java, we would write getters and setters to set/get the
> attribute bar. However, in Python, we normally create a class like so;
>
> class Foo(object):
>     bar = 0
>     ...
>
> And we usually don't write any getters/setters (though they exist in
> Python, I have not seen much projects making use of it).

The vast majority of getters and setters do nothing other than get/set
the field they belong to. They exist only to allow the *possibility*
of doing something else at some point far in the future. That's a ton
of undesirable boilerplate for little real benefit.

In Python, OO designers are able to get away with not using getters
and setters because we have properties. You can start with an
attribute, and if you later want to change the means of getting and
setting it, you just replace it with a property. The property lets you
add any logic you want, and as far as the outside world is concerned,
it still just looks like an attribute.

> We can easily encapsulate (data hiding) Foo's class using the '_'
> (underscore) when creating a new attribute, however, this would require
> all attributes to have a underscore.

A single leading underscore is just a naming convention, not true data
hiding, which isn't really possible in Python. Even the double
underscore only does name mangling, not true data hiding. It's meant
to prevent *accidental* naming collisions. You can still easily access
the attribute from outside the class if you're determined to.

This all boils down to the fact that code inside a method has no
special privilege over external code. If you could hide data so well
that external code really couldn't access it, then you wouldn't be
able to access it either.

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


Thread

Re: Encapsulation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-10 08:27 -0700
  Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-10 16:45 -0800
    Re: Encapsulation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-11 08:47 -0700
      Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-11 18:39 -0800
        Re: Encapsulation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-12 09:44 -0700
          Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-12 19:11 -0800
          Re: Encapsulation in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-13 21:11 +1100
            Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 10:32 -0700
              Re: Encapsulation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-14 15:09 -0600
              Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-14 21:23 +0000
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-14 22:07 +0000
                Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-14 22:20 +0000
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-14 22:40 +0000
                Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-14 23:19 +0000
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-14 23:56 +0000
                Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-15 11:12 +1100
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-15 00:54 +0000
                Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-15 11:58 +1100
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-15 01:22 +0000
                Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-15 13:02 +1100
                Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 00:28 +0000
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-15 01:10 +0000
                Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-15 12:23 +1100
                Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 04:41 +0000
                Re: Encapsulation in Python rurpy@yahoo.com - 2016-03-14 17:17 -0700
                Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-15 11:25 +1100
                Re: Encapsulation in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-15 13:06 +1100
                Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-15 13:14 +1100
                Re: Encapsulation in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-15 13:40 +1100
                Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 21:08 -0700
                Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 00:47 +0000
                Re: Encapsulation in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-15 13:46 +1100
                Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-15 11:56 +1100
                Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 04:36 +0000
                Re: Encapsulation in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-15 13:01 +1100
                Re: Encapsulation in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 04:45 +0000
                Re: Encapsulation in Python Christian Gollwitzer <auriocus@gmx.de> - 2016-03-15 22:02 +0100
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-16 00:39 +0000
                Re: Encapsulation in Python BartC <bc@freeuk.com> - 2016-03-16 22:58 +0000
        Re: Encapsulation in Python sohcahtoa82@gmail.com - 2016-03-14 11:11 -0700
          Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 23:09 -0700
      Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-11 18:56 -0800
    Re: Encapsulation in Python Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-12 13:52 +1300
      Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-12 08:49 -0800
        Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-13 08:10 +1100
          Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-12 19:36 -0800
            Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-13 15:05 +1100
        Re: Encapsulation in Python Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-14 12:35 +1300
          Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-14 15:55 -0700

csiph-web