[Python-Dev] Declaring setters with getters

Guido van Rossum guido at python.org
Wed Oct 31 19:09:06 CET 2007


On 10/31/07, Phillip J. Eby <pje at telecommunity.com> wrote:
> At 10:08 AM 10/31/2007 -0700, Guido van Rossum wrote:
> >I've come up with a relatively unobtrusive pattern for defining
> >setters. Given the following definition:
> >
> >def propset(prop):
> >     assert isinstance(prop, property)
> >     def helper(func):
> >         return property(prop.__get__, func, func, prop.__doc__)
> >     return helper
>
> Shouldn't that be property(prop.fget, func, prop.fdel, prop.__doc__),

Of course. (Though my code works too. :-)

> and have a matching propdel decorator?

Yes, though I think that if prop.fdel is None, we could use func in both slots.

> Apart from those extremely small nits, a big +1, and I expect to
> swipe this idea more or less immediately.  :)

Be my guest. I should patent this; it's apparently non-obvious. :-) (I
can't recall why I didn't think of this sooner, and why nobody else
did.)

> >I'd also like to change property so that the doc string defaults to
> >the doc string of the getter.
>
> +1 also; I thought it did this already, but am now disappointed to
> find it doesn't.  :)

Patch for 2.6 anyone?

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list