There was a beautiful way to organize class property in frame of one function, by using the 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())
But now apply has been deprecated.
Is there any possibility to achieve such simplicity and readability for property, with new, came instead “extended call syntax”?
My approach is same as Anurag’s, but, I don’t now witch one is better, please look:
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()
To mark a function or method as deprecated, wrap it in the deprecated() decorator. This does several things for you: The docstring of the wrapped function will have details appended to it from the arguments you set on deprecated() .
Usage of a module may be 'deprecated', which means that it may be removed from a future Python release. The rationale for deprecating a module is also collected in this PEP. If the rationale turns out faulty, the module may become 'undeprecated'.
And you should be able to indicate deprecation when the function is imported from the module. Decorator would be a right tool for that.
Is there any possibility to achieve such simplicity and readability for property
The new Python 2.6 way is:
@property
def myattr(self):
"""This is the doc string."""
return self._half * 2
@myattr.setter
def myattr(self, value):
self._half = value / 2
@myattr.deleter
def myattr(self):
del self._half
:) that is a clever user of apply, though i am not sure if there are any pitfalls?
anyway you can do this
class Example(object):
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())
myattr = myattr()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With