Python style typically prefers that fields be public, but when that is not possible or does not make sense, it uses properties instead of traditional getters and setters. For example, the traditional getter/setter pair
def get_value(self):
return self._value
def set_value(self, value):
self._value = value;
would be written as a property as follows:
@property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value;
The @property
is a method decorator that tells Python that the following method is a property,
and the @value.setter
decorator associates the setter method with the getter. (Python discourages
having only a setter.) The get_
and set_
prefixes are consequently dropped.
The property methods would be called not as:
some_object.set_value(4)
print(some_object.get_value())
But by writing
some_object.value = 4
print(some_object.value)
Notice that Python hides the fact that methods are being called to make using properties indistinguishable from using public fields.
Fill in the placeholders so that the actual outputs match the expected outputs.