通过使用apply decorator,有一种漂亮的方法可以在一个函数的框架中组织类属性.
class Example(object):
@apply
def myattr():
doc = """This is the doc string."""
def fget(self):
return self._half * 2
def fset(self, value):
self._half = value / 2
def fdel(self):
del self._half
return property(**locals())
Run Code Online (Sandbox Code Playgroud)
但现在申请已被弃用.
是否有可能实现属性的这种简单性和可读性,使用new,而不是"扩展调用语法"?
我的方法与Anurag的方法相同,但是,我现在没有一个更好,请看:
def prop(f):
return property(**f())
class A(object):
@prop
def myattr():
def fget(self):
return self._myattr
def fset(self, value):
self._myattr = value
return locals()
Run Code Online (Sandbox Code Playgroud) python ×1