I stumbled across the below example of using getters and setters in a different question Preferred way of defining properties in Python: property decorator or lambda?
Since python has implicit getters and setters, I wonder what the reason is to define them explicitly as below. Is there any advantage in those examples or does it only make sense when the getters/setters involve anything more complicated than the simplified examples below?
class Bla(object):
def sneaky():
def fget(self):
return self._sneaky
def fset(self, value):
self._sneaky = value
return locals()
sneaky = property(**sneaky())
Recent versions of python enhanced the decorator approach:
class Bla(object):
@property
def elegant(self):
return self._elegant
@elegant.setter
def elegant(self, value):
self._elegant = value