Accessors in Python (getters and setters)
Gerhard Fiedler
gelists at gmail.com
Thu Jul 20 13:37:06 EDT 2006
On 2006-07-20 09:40:31, Bruno Desthuilliers wrote:
>> I'm not sure, but there's one thing that has a potential to be the real
>> issue: what's the common way to create a property that is read-write
>> for the implementation and "read-only" for the interface?
>
> class Foo(object):
> @apply
> def _imp():
> def fget(self):
> # code here
> def fset(self, val):
> # code here
> return property(**locals())
>
> @apply
> def api():
> def fget(self):
> return self._imp
> def fset(self, val):
> raise SomeException('read-only, sorry')
> return property(**locals())
Thanks a lot. This looks similar to what Steve Holden wrote. (And yes,
Steve, you understood exactly what I meant :)
My hunch is that this is what mystilleef might have wanted to say all
along; it looks quite similar to what he seemed to try to express. It's
basically the Python synonym of creating a public accessor (api) for a
private attribute (_imp) -- in this case to make the API read-only while
maintaining the implementation read-write, but I'm sure there are other
uses for such a structure.
> Note that a read-only property named 'is_active' returning the value of
> an attribute named '_is_active' doesn't prevent direct access to
> '_is_active' attribute, neither from the class nor from the client code.
Right; I already understood that :)
I just thought one part of this whole thread was that accessors are not
necessary in Python. However, it seems that this case -- different
restrictions (or actions) for access from implementation vs through the API
(which is probably one of the most common reasons for using accessors in
other languages) -- in Python also can only be handled by adding a separate
accessor. (This of course in Python is implemented as a computed
attribute.)
The above illustrated technique doesn't look so different from a C++ or
Java code with public set/getApi() accessors acting on a private imp
attribute (or public set/getIsActive() accessors acting on a private
isActive attribute -- to use the other, similar example that operates with
the names this thread likes to use :)
Do I seem to understand this correctly?
Thanks,
Gerhard
More information about the Python-list
mailing list