Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Mezger Newsgroups: comp.lang.python Subject: Encapsulation in Python Date: Thu, 10 Mar 2016 10:41:25 -0300 Lines: 74 Message-ID: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104518 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --XUlknnbDtSt330lq3s9bNoe6cwDKG2aoN Content-Type: multipart/mixed; boundary="sU9QSI12JhsOrIRIhAGO81dPImwTiM5CL" From: Ben Mezger To: python-list@python.org Message-ID: <56E17985.7060002@benmezger.nl> Subject: Encapsulation in Python --sU9QSI12JhsOrIRIhAGO81dPImwTiM5CL Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 =3D 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 =3D 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 --sU9QSI12JhsOrIRIhAGO81dPImwTiM5CL-- --XUlknnbDtSt330lq3s9bNoe6cwDKG2aoN Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQIcBAEBCgAGBQJW4XmFAAoJENGNPNyhY4OGiz8QALLJDfszKvN6gK1uYsNikpKK 45c7eUCdoLgnGoKyx8l3Sey8J+ElpM1FbRsOIwR/V0LtZVRwfsfRPE5J64wyHHMl 4zFyZo+M/YV35VgyQoKqJsOYIPCjpFMpl4SRs4AuIrdDD/zPM9D8GasmBDEt+Rxa GEC+a7qSYHn0xc0H68WZ1Oz4DnPb4vzwRpHAC7C0ReQ2WBMUQj07Y4h4ckDPx/Sf cnCJKMEfQ/goC7r0BzNCfiJVlX9hbIfRpJSpuBB9BmFprwwCF22TARD8tsAKdVhU SenpDjko7EnnZa+4fFQBSERVRlkMQOac9TBfoFYf0XhJ23W00zWvbY29TAtGt9al fjJ+IXnfJV8L8pus5v0u7p+T9jlFzUKJW3pPwbO1I6zLEMiMOXeKwjgeg7oEANvg wy/2aDJYKopa4G1phVh1PDuNANbAuQ24Tsxl11ZibwqHbLm2Y6uMjQg91o1tdnvB Z1lhJt8hCLLfvwxYaz3schkKOm2A76wUwmLoMF7zOWAqosvmqSnrU2Og9Iy7oSJ9 F40OujD7N4oSeDgILk0m9yN+nvtf/nWDtMdu6qbuDvX9uvOktgc1bccMh2eqmgd5 RGjw6zqBwOFZhCfDzwE5EbvlMXCYr8iGKiEZEXDdqU7v2npdYuqG9pjIvMnNxALo aIOYSoWaDxU1ebhsgW4K =gaHY -----END PGP SIGNATURE----- --XUlknnbDtSt330lq3s9bNoe6cwDKG2aoN--