Is it just me, or is the Python syntax for defining properties of classes a bit cumbersome? Like this:
class Example:
@property
def my_property(self):
return self.real_value
@my_property.setter
def my_property(self, value):
self.real_value = value
My main problem with this is that the name of the property, my_property above, has to be repeated 3 times. This does not seem very Pythonic, and is a pain when I want to rename the property, or want to create several properties - copy/paste or a snippet, then fix the name 3 times. People with editors that have better refactoring support might not mind so much, but in Pythonista it is painful.
So, I have started to routinely use a decorator like this:
def prop(func):
return property(func, func)
With it, defining a new property goes like this:
class Example:
@prop
def my_property(self, *value):
if value:
self.real_value = value[0]
else:
return self.real_value
A bit ugly, same amount of lines, but easy to copy/paste or refactor, as the name of the property is there only once.
I do not know if anyone else cares, but opinions and alternate approaches are very welcome.