@x.setter property implementation

Floris Bruynooghe floris.bruynooghe at gmail.com
Thu Apr 10 10:37:30 EDT 2008


On Apr 7, 2:19 pm, "Andrii V. Mishkovskyi" <misho... at gmail.com> wrote:
> 2008/4/7, Floris Bruynooghe <floris.bruynoo... at gmail.com>:
>
>
>
> >  Have been grepping all over the place and failed to find it.  I found
> >  the test module for them, but that doesn't get me very far...
>
> I think you should take a look at 'descrobject.c' file in 'Objects' directory.


Thanks, I found it!  So after some looking around here was my
implementation:

class myproperty(property):
    def setter(self, func):
        self.fset = func

But that doesn't work since fset is a read only attribute (and all of
this is implemented in C).

So I've settled with the (nearly) original proposal from Guido on
python-dev:

def propset(prop):
    assert isinstance(prop, property)
    @functools.wraps
    def helper(func):
        return property(prop.fget, func, prop.fdel, prop.__doc__)
    return helper

The downside of this is that upgrade from 2.5 to 2.6 will require code
changes, I was trying to minimise those to just removing an import
statement.

Regards
Floris



More information about the Python-list mailing list