Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104620
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Encapsulation in Python |
| Date | 2016-03-11 08:08 -0700 |
| Message-ID | <mailman.23.1457708968.26429.python-list@python.org> (permalink) |
| References | <56E17985.7060002@benmezger.nl> <87a8m5pdl6.fsf@handshake.de> |
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 comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Encapsulation in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-11 08:08 -0700
csiph-web