Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: Encapsulation in Python Date: Thu, 10 Mar 2016 14:04:39 +0000 Lines: 66 Message-ID: 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 5P+rZ/sY7cOE+/qPDVbNuAQlChKOPRuy27PMZmDmhDvw== Return-Path: 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; 'from:addr:yahoo.co.uk': 0.05; 'attributes': 0.07; '0),': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; '"this': 0.13; 'suggest': 0.15; 'applies': 0.15; 'java,': 0.15; '(data': 0.16; 'attribute,': 0.16; 'attribute;': 0.16; 'directly?': 0.16; 'foo(object):': 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; 'programmer': 0.18; 'python?': 0.18; 'variable': 0.18; 'language': 0.19; 'all,': 0.20; "aren't": 0.22; 'bar.': 0.22; 'lawrence': 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; 'wasting': 0.29; 'creating': 0.30; 'convention': 0.30; 'normally': 0.30; 'programmers': 0.30; '[1]': 0.32; 'language.': 0.32; 'class': 0.33; 'usually': 0.33; 'foo': 0.33; 'int': 0.33; 'except': 0.34; 'worked': 0.34; 'acceptable': 0.35; 'exist': 0.35; 'according': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; '(with': 0.38; 'itself': 0.38; 'no,': 0.38; 'why': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'mark': 0.40; 'your': 0.60; 'charset:windows-1252': 0.62; 'making': 0.62; 'great': 0.63; 'our': 0.64; 'pythonistas,': 0.84; 'studying': 0.84; 'subject:skip:E 10': 0.96 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 80.234.129.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104520 On 10/03/2016 13:41, 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; I suggest that you read http://dirtsimple.org/2004/12/python-is-not-java.html and http://dirtsimple.org/2004/12/java-is-not-python-either.html > > 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). Python programmers in the main see getters/setters as unneeded, time wasting boilerplate. > > 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. No, this is merely a convention that can be worked around if you really want to. The same applies to the use of the double 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? You have misunderstood. The '_' is just a convention that says, "this is private, please keep your mitts off". There is nothing to stop a programmer from using it. > > Regards, > > Ben Mezger > > [1] - http://stackoverflow.com/q/4555932 > I suggest that you reread the stackoverflow link that you've quoted, and take great notice of the response from Lennart Regebro, even if it has been downvoted. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence