Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. class Celsius:
  2. def __init__(self, temperature = 0):
  3. self._temperature = temperature
  4.  
  5. def to_fahrenheit(self):
  6. return (self.temperature * 1.8) + 32
  7.  
  8. @property
  9. def temperature(self):
  10. print("Getting value")
  11. return self._temperature
  12.  
  13. @temperature.setter
  14. def temperature(self, value):
  15. if value < -273:
  16. raise ValueError("Temperature below -273 is not possible")
  17. print("Setting value")
  18. self._temperature = value
  19.  
  20. Properties provide a way to have both getters and setters with backwards compatibility to just accessing the variable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement