Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104620 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2016-03-11 08:08 -0700 |
| Last post | 2016-03-11 08:08 -0700 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Encapsulation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-11 08:08 -0700
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-03-11 08:08 -0700 |
| Subject | Re: Encapsulation in Python |
| Message-ID | <mailman.23.1457708968.26429.python-list@python.org> |
On Fri, Mar 11, 2016 at 2:29 AM, dieter <dieter@handshake.de> wrote: > 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. Pythonically, one would use a property to do this. You don't need anything so advanced as a metaclass. Using either approach though, there is no place you can hide the real attributes where the caller isn't capable of getting at them.
Back to top | Article view | comp.lang.python
csiph-web