when learning python property decorator in this link, I stumbled upon following lines of code:
class Celsius:
def __init__(self, temperature = 0):
self.temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
def get_temperature(self):
print("Getting value")
return self._temperature
def set_temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = value
temperature = property(get_temperature,set_temperature)
this code basically let you modify/add restraint to a Celsius
object, without having anybody who inherited Celsius
class refactor their code.
why does it define a class variable temperature
first, rather than just let self.temperature = property(get_temperature,set_temperature)
and done?
EDIT: Due to conflict opinions in comments, I will now restore the code to original state, regardless if there is a typo, to make it easy for people read this afterwards.