Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Encapsulation in Python Date: Fri, 11 Mar 2016 10:29:25 +0100 Lines: 66 Message-ID: References: <56E17985.7060002@benmezger.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de xO/B76ggb/qINVIrPqaozgV6uyo6GOeHzAGiGj9uuE3g== Cancel-Lock: sha1:4ZRe+uZ4AYdbCdQfaC/zk42K8N0= 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; 'method.': 0.05; 'attributes': 0.07; '"python"': 0.09; '0),': 0.09; 'derived': 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; 'java,': 0.15; '"python",': 0.16; '(data': 0.16; '(other': 0.16; 'attribute,': 0.16; 'attribute;': 0.16; 'code).': 0.16; 'constructs': 0.16; 'distinction': 0.16; 'foo(object):': 0.16; 'hint': 0.16; 'instance:': 0.16; 'java.': 0.16; 'metaclass': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'so;': 0.16; 'underscore.': 0.16; 'attribute': 0.18; 'python?': 0.18; 'java': 0.22; 'bar.': 0.22; 'enforce': 0.22; 'oriented': 0.22; 'parameter': 0.22; 'defined': 0.23; 'unlike': 0.23; 'thus': 0.24; 'all.': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'define': 0.27; 'wonder': 0.27; 'towards': 0.28; '(maybe': 0.29; 'declared': 0.29; 'expose': 0.29; 'forces': 0.29; 'publicly': 0.29; 'thus,': 0.29; 'classes': 0.30; 'creating': 0.30; 'code': 0.30; 'class.': 0.30; 'normally': 0.30; 'class': 0.33; 'usually': 0.33; 'foo': 0.33; 'instances': 0.33; 'int': 0.33; 'policies': 0.33; 'similar': 0.33; 'quickly': 0.34; 'except': 0.34; 'could': 0.35; 'acceptable': 0.35; 'exist': 0.35; 'i.e.': 0.35; 'instance': 0.35; 'level': 0.35; 'problem.': 0.35; 'according': 0.36; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; '(with': 0.38; 'itself': 0.38; 'easily': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'your': 0.60; 'real': 0.62; 'making': 0.62; 'more': 0.63; 'between': 0.65; 'gain': 0.82; 'studying': 0.84; 'subject:skip:E 10': 0.96 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57b38999.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) 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:104602 Ben Mezger writes: > 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). If you expose publicly both a "setter" and a "getter" what is the gain not to make the attribute public in the first case (other than cluttering up your code). I very much like the "Eiffel" view that there should be no distinction between an attribute access and the call of a zero parameter method. Thus, there are no "getter"s at all. Unlike "Python", "Eiffel" is very interested in encapsulation: thus attributes can be private, limited to derived classes or public -- and they can be readonly. "Python" is much less interested in encapsulation - and more in the ability to quickly get a solution to a practical problem. > 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. And it is only a hint towards users of the class. Nothing forces a piece of code to use it as public. > 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? If you are really interested to enforce Java encapsulation policies (access to attributes via "getter/setter" only), you will need to use your own "metaclass". The "metaclass" has a similar relation to a class as a class to an instance: i.e. it constructs a class. During the class construction, your "metaclass" could automatically define "getter/setter" methods for declared class attributes and hide the real attributes (maybe by prefixing with "__"). Of course, class level (non-method) attributes are rare; most attributes of Python instances are not defined at the class level but directly at the instance level - and the metaclass would need to define "__setattr__" and "__getattribute__" to control access to them. However, I think you should not use Python in cases where you need strong encapsulation.