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


Groups > comp.lang.python > #104624

Re: Encapsulation in Python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From jmp <jeanmichel@sequans.com>
Newsgroups comp.lang.python
Subject Re: Encapsulation in Python
Date Fri, 11 Mar 2016 17:42:49 +0100
Lines 48
Message-ID <mailman.27.1457714598.26429.python-list@python.org> (permalink)
References <56E17985.7060002@benmezger.nl>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de dj0Dux0y4ezE11rgfZ4uFAXeSfZw1qAmpMgCr98tA9sQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:Python': 0.05; 'attributes': 0.07; '0),': 0.09; 'mess': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; "they've": 0.09; 'python': 0.10; 'python.': 0.11; 'java,': 0.15; '(data': 0.16; '(good)': 0.16; 'attribute,': 0.16; 'attribute;': 0.16; 'directly?': 0.16; 'foo(object):': 0.16; 'it".': 0.16; 'java.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'so;': 0.16; 'underscore.': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'stick': 0.18; 'python?': 0.18; 'variable': 0.18; 'all,': 0.20; "aren't": 0.22; 'bar.': 0.22; 'oriented': 0.22; 'header:In-Reply- To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'wonder': 0.27; 'expose': 0.29; 'creating': 0.30; 'convention': 0.30; 'normally': 0.30; '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; 'there': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'things': 0.38; '(with': 0.38; 'itself': 0.38; 'why': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'received:194': 0.61; 'charset:windows-1252': 0.62; 'making': 0.62; 'strictly': 0.64; 'story': 0.65; 'flag.': 0.84; 'studying': 0.84; 'subject:skip:E 10': 0.96
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host paris.sequans.com
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
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:104624

Show key headers only | View raw


On 03/10/2016 02:41 PM, Ben Mezger 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;
>      ...
>
> 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

Strictly speaking there is not such things as public/private attributes 
in python. All attributes are public.

'_' is just a (good) convention to tell the class users "don't mess with 
this attribute, don't read it nor write it".

And the python way is to stick to this, trust your users to not use your 
'private' attributes they've been warned.

Short story : in Python we don't hide, we flag.

JM

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


Thread

Re: Encapsulation in Python jmp <jeanmichel@sequans.com> - 2016-03-11 17:42 +0100

csiph-web