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


Groups > comp.lang.python > #104518

Encapsulation in Python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Mezger <me@benmezger.nl>
Newsgroups comp.lang.python
Subject Encapsulation in Python
Date Thu, 10 Mar 2016 10:41:25 -0300
Lines 74
Message-ID <mailman.132.1457617425.15725.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="XUlknnbDtSt330lq3s9bNoe6cwDKG2aoN"
X-Trace news.uni-berlin.de IX8peGBRQSuWQwj/vsoN4gQCYzjU3gipNfoJMKZCEbyg==
Return-Path <me@benmezger.nl>
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; '0),': 0.09; 'java,': 0.15; '(data': 0.16; 'attribute,': 0.16; 'attribute;': 0.16; 'directly?': 0.16; 'filename:fname piece:signature': 0.16; 'foo(object):': 0.16; 'java.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'so;': 0.16; 'underscore.': 0.16; 'attribute': 0.18; 'python?': 0.18; 'variable': 0.18; 'all,': 0.20; "aren't": 0.22; 'bar.': 0.22; 'oriented': 0.22; "i've": 0.25; 'wonder': 0.27; 'expose': 0.29; 'creating': 0.30; 'normally': 0.30; '[1]': 0.32; 'class': 0.33; 'usually': 0.33; 'foo': 0.33; 'int': 0.33; 'except': 0.34; 'acceptable': 0.35; 'exist': 0.35; 'according': 0.36; 'should': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'received:org': 0.37; '(with': 0.38; 'itself': 0.38; 'why': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'making': 0.62; 'studying': 0.84; 'subject:skip:E 10': 0.96
X-Virus-Scanned amavisd-new at heinlein-support.de
X-Mailman-Approved-At Thu, 10 Mar 2016 08:43:45 -0500
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:104518

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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;
    ...

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).

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.
According to this answer [1], it's acceptable to to expose your
attribute directly (Foo.bar = 0), so I wonder where the encapsulation
happens in Python? If I can access the attribute whenever I want (with
the except of using a underscore), what's the best way to encapsulate a
class in Python? Why aren't most of the projects not using
getters/setters and instead they access the variable directly?

Regards,

Ben Mezger

[1] - http://stackoverflow.com/q/4555932

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


Thread

Encapsulation in Python Ben Mezger <me@benmezger.nl> - 2016-03-10 10:41 -0300
  Re: Encapsulation in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-11 20:28 +1100
    Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-11 16:45 -0800
      Re: Encapsulation in Python dieter <dieter@handshake.de> - 2016-03-12 10:42 +0100
        Re: Encapsulation in Python Rick Johnson <rantingrickjohnson@gmail.com> - 2016-03-12 07:03 -0800
      Re: Encapsulation in Python Chris Angelico <rosuav@gmail.com> - 2016-03-13 00:42 +1100

csiph-web