[Baypiggies] April snippets meeting - property2

Shannon -jj Behrens jjinux at gmail.com
Wed Apr 18 01:14:11 CEST 2007


On 4/14/07, Drew Perttula <drewp at bigasterisk.com> wrote:
> Here is the property() variation that works as a decorator, but seems to
> continue to work the same as old property(). The big trick is
> distinguishing property(justOneGetter) from
> property(aFunctionToCallToGetTheArgs). Based on Doug's suggestion, I am
> inspecting the incoming function's signature to guess which mode the
> user wants.
>
>
> import inspect
>
> def property2(fget=None, fset=None, fdel=None, doc=None):
>      funcs = dict(fset=fset, fget=fget, fdel=fdel, doc=doc)
>      if fget is not None:
>          argNames = inspect.getargspec(fget)[0]
>          if not argNames:
>              funcs = fget()
>
>      return property(**funcs)
>
> class Foo(object):
>      def x():
>          def fset(self, v):
>              self._x = v
>          def fget(self):
>              return self._x
>          return locals()
>      x = property2(**x())
>
>      @property2
>      def y():
>          def fget(self):
>              return "why"
>          return locals()
>
>      def z_fget(self):
>          return 'zee'
>      z = property2(z_fget)
>
> f = Foo()
> f.x = 5
> assert f.x == 5
> assert f.y == 'why'
> assert f.z == 'zee'

I continue to wonder if there's any chance Python's property function
will be updated so that it can also act as a function decorator.
(nudge, nudge ;)

Best Regards,
-jj

-- 
"'Software Engineering' is something of an oxymoron.  It's very
difficult to have real engineering before you have physics, and there
isn't anything even close to a physics for software." -- L. Peter
Deutsch


More information about the Baypiggies mailing list