-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat-property.py
55 lines (48 loc) · 1.65 KB
/
at-property.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import constants as Site
DZ = Site.Core()
DZ.line("DZ module initiated")
#import inspect
import pprint
#########################DZ.line("HEADER ##########################
# problem... refactoring a class that had getters/setters added...
class Celsius:
def __init__(self, temperature=0):
self.set_temperature(temperature)
def to_fahrenheit(self):
return (self.get_temperature() * 1.8) + 32
# getter method
def get_temperature(self):
print("Getting value...")
return self._temperature
# setter method
def set_temperature(self, value):
print("Setting value...")
if value < -273.15:
raise ValueError("Temperature below -273.15 is not possible.")
self._temperature = value
# creating a property object
temperature = property(get_temperature, set_temperature)
# property(fget=None, fset=None, fdel=None, doc=None)
# fget gets val of attr
# fset sets val of attr
# fdel deletes attr
# doc is a string(comment)
# You can manually set the methods for property later...
# temperature = property()
# temperature = temperature.getter(get_temperature)
# temperature = temperature.setter(set_temperature)
# eq ^^^
DZ.line("CONTINUE TO: https://www.programiz.com/python-programming/property")
# to this will be a nightmare... right
human = Celsius(37)
print(human.get_temperature())
print(human.to_fahrenheit())
human.set_temperature(-30)
print(human.to_fahrenheit())
DZ.line("Old code to refactor")
### OLD CODE BEGIN ###
human2 = Celsius()
human2.temperature = 0
print(human2.temperature)
print(human2.to_fahrenheit())
### OLD CODE END ###