
The property decorator, with its .setter and .deleter chaining, is a bit cumbersome and repetitive. If we can add `.apply` as a method on property that calls a function that returns the fget/fset/fdel/doc arguments, it would simplify instantiating the descriptor. For example: @property.apply def attr(): def fget(self): pass def fset(self, value): pass def fdel(self): pass return (fget, fset, fdel, "doc") instead of @property def attr(self): pass @attr.setter def attr(self, value): pass @attr.deleter def attr(self): pass An example implementation using `fproperty` exists at https://github.com/serwy/fproperty

On Sat, Aug 08, 2020 at 10:22:20PM -0000, Roger Serwy wrote:
You don't need an apply method for that, property already takes four arguments, so you can write: def factory(): def fget(self): pass def fset(self, value): pass def fdel(self): pass return (fget, fset, fdel, "doc") attr = property(*factory()) # optional: del factory but frankly, having to write the property getters and setters as nested functions is (in my opinion) uglier looking and more annoying than the usual property chaining version. So if you want to use this with decorator notation, you need a one-line helper: def myproperty(func): return property(*func()) and change the name of "factory" to "attr". -- Steven

That's an excellent point about argument unpacking. Thank you for your feedback, I'll consider this idea resolved.

On Sat, Aug 08, 2020 at 10:22:20PM -0000, Roger Serwy wrote:
You don't need an apply method for that, property already takes four arguments, so you can write: def factory(): def fget(self): pass def fset(self, value): pass def fdel(self): pass return (fget, fset, fdel, "doc") attr = property(*factory()) # optional: del factory but frankly, having to write the property getters and setters as nested functions is (in my opinion) uglier looking and more annoying than the usual property chaining version. So if you want to use this with decorator notation, you need a one-line helper: def myproperty(func): return property(*func()) and change the name of "factory" to "attr". -- Steven

That's an excellent point about argument unpacking. Thank you for your feedback, I'll consider this idea resolved.
participants (2)
-
Roger Serwy
-
Steven D'Aprano