On Apr 10, 5:09 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > On Apr 10, 3:37 pm, Floris Bruynooghe <[EMAIL PROTECTED]> > wrote: > > > > > On Apr 7, 2:19 pm, "Andrii V. Mishkovskyi" <[EMAIL PROTECTED]> wrote: > > > > 2008/4/7, Floris Bruynooghe <[EMAIL PROTECTED]>: > > > > > 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 > > Here's an implementation of prop.setter in pure python < 2.6, but > using sys._getframe, and the only test performed is the one below :) > > import sys > > def find_key(mapping, searchval): > for key, val in mapping.iteritems(): > if val == searchval: > return key > > _property = property > > class property(property): > def setter(self, fset): > cls_ns = sys._getframe(1).f_locals > propname = find_key(cls_ns, self) > # if not propname: there's a problem! > cls_ns[propname] = property(self.fget, fset, > self.fdel, self.__doc__) > return fset > # getter and deleter can be defined the same way! > > # -------- Example ------- > > class Foo(object): > @property > def bar(self): > return self._bar > @bar.setter > def setbar(self, x): > self._bar = '<%s>' % x > > # -------- Interactive test ----- > > >>> foo = Foo() > >>> foo.bar = 3 > >>> foo.bar > '<3>' > >>> foo.bar = 'oeufs' > >>> foo.bar > '<oeufs>' > > Having fun'ly yours,
Neat! Unfortunatly both this one and the one I posted before work when I try them out on the commandline but both fail when I try to use them in a module. And I just can't figure out why. Floris -- http://mail.python.org/mailman/listinfo/python-list