[Baypiggies] April snippets meeting - property2
Drew Perttula
drewp at bigasterisk.com
Sat Apr 14 19:44:13 CEST 2007
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'
More information about the Baypiggies
mailing list