[Python-Dev] Declaring setters with getters (original) (raw)
Guido van Rossum guido at python.org
Thu Nov 1 15:01:42 CET 2007
- Previous message: [Python-Dev] Declaring setters with getters
- Next message: [Python-Dev] Declaring setters with getters
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 10/31/07, glyph at divmod.com <glyph at divmod.com> wrote:
As long as we're all tossing out ideas here, my 2ยข. I vastly prefer this:
On 02:43 am, steven.bethard at gmail.com wrote: >On 10/31/07, Fred Drake <fdrake at acm.org> wrote: >> @property.set >> def attribute(self, value): >> self.ignored = value to this: > @property.set(attribute) > def attribute(self, value): > self.ignored = value since I don't see any additional expressive value in the latter, and it provides an opportunity to make a mistake.
I was expecting this would be brought up, but that just ain't gonna happen. If you don't repeat the name, the decorator has to root around in the surrounding scope, which is fraught with peril. Solutions based on sys._getframe() have been around for years (e.g. several the Cookbook recipes) and if I had approved of that technique I would have adopted one long ago.
However I don't approve of it. It has always been and will always continue to be my position that these are semantically unkosher, because it means that you can't wrap them in convenience functions or invoke them in different contexts, and that means that the semantics are hard to explain.
If you really want another argument, repeating the property name actually does have an additional use case: you can have a read-only property with a corresponding read-write property whose name differs. E.g.
class C(object):
@property def encoding(self): return ...
@propset(encoding) def encoding_rw(self, value): ...
c = C() c.encoding = "ascii" # Fails c.encoding_rw = "ascii" # Works
I've seen people do this in the filesystem: a read-only version that may be cached or replicated, and a separate writable version. Reading the writable version works too, of course.
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] Declaring setters with getters
- Next message: [Python-Dev] Declaring setters with getters
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]