Here is a brain dump of the example you can find in my github. As always, you can find a couple of tests as well as the code.
class SetterGetter():
def __init__(self, x):
self._x = x
@property
def x(self):
return self._x
@x.setter
def x(self, value):
print('x\'s value has been set to {0} and can\'t be changed'.format(self._x))
@x.getter
def x(self):
print('prepare to catch x\'s value!')
return self._x
if __name__ == '__main__':
settergetter = SetterGetter(10)
print(settergetter.x)
settergetter.x = 5
print(settergetter.x)
The output is:
prepare to catch x's value!
10
x's value has been set to 10 and can't be changed
prepare to catch x's value!
10
So as you can see, what property allows you to do is to transform the x method into a property (read more about it in the official documentation). Then, setter and getter can be used to control setting and getting the property's value. In this example, I'm protecting the property x from being set outside the constructor.