Skip to content

Commit 74cc5bc

Browse files
committed
adding the property decorator
1 parent caa982a commit 74cc5bc

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# "property" decorator
2+
# "property" decorator is a built-in decorator in Python which is helpful in
3+
# defining the properties without manually calling the built-in
4+
# function property(). The decorator allows you to create a property object or
5+
# attribute of a class, same as the "property()"
6+
# The difference is that the methods that have the property function as a
7+
# decorator will not be protected members because the methods that have the
8+
# getter decorator will be called from outside the class.
9+
10+
class TemperatureConvertor:
11+
12+
"""Fahrenheit to Celsius"""
13+
# 1. Add the property decorator on the get function:
14+
@property
15+
def temp_get_fahrenheit(self):
16+
"""Get the temperature in Fahrenheit"""
17+
return self._fahrenheit
18+
19+
# 2. Define the setter decorator, we can keep the same name as the
20+
# get function.
21+
@temp_get_fahrenheit.setter
22+
def temp_set_fahrenheit(self, fahrenheit):
23+
"""Set the temperature in Fahrenheit"""
24+
self._fahrenheit = fahrenheit
25+
26+
# 3. We can add a deleter function
27+
@temp_get_fahrenheit.deleter
28+
def temp_del_fahrenheit(self):
29+
del self._fahrenheit
30+
31+
def convert_to_celsius(self):
32+
celsius = (self._fahrenheit - 32) * 5/9
33+
print(f"The {self._fahrenheit}°F is:")
34+
return f"{round(celsius, 0)}°C"
35+
36+
"""Celsius to Fahrenheit"""
37+
@property
38+
def temp_get_celsius(self):
39+
"""Get the temperature in Celsius"""
40+
return self._celsius
41+
42+
@temp_get_celsius.setter
43+
def temp_set_celsius(self, celsius):
44+
"""Set the temperature in Celsius"""
45+
self._celsius = celsius
46+
47+
@temp_get_celsius.deleter
48+
def temp_del_celsius(self):
49+
del self._celsius
50+
51+
def convert_to_fahrenheit(self):
52+
fahrenheit = self._celsius * 9/5 + 32
53+
print(f"The {self._celsius}°C is:")
54+
return f"{round(fahrenheit, 0)}°F"
55+
56+
57+
# Creating an instance of the the class
58+
temp = TemperatureConvertor()
59+
60+
# celsius_or_fahrenheit = input("Choose C or F: ")
61+
# if celsius_or_fahrenheit == "F":
62+
# fahrenheit = float(input("Insert temperature in Fahrenheit: "))
63+
# temp.temp_set_fahrenheit = fahrenheit
64+
# print(temp.convert_to_celsius())
65+
66+
# elif celsius_or_fahrenheit == "C":
67+
# celsius = float(input("Insert temperature in Celsius: "))
68+
# temp.temp_set_celsius = celsius
69+
# print(temp.convert_to_fahrenheit())
70+
71+
# else:
72+
# print("Wrong input, please insert C or F in capital letters")
73+
74+
75+
temp.temp_set_celsius = 90
76+
# print(temp.convert_to_fahrenheit())
77+
78+
# Get the property:
79+
# print(temp.temp_get_celsius)
80+
81+
# Deleting the property:
82+
del temp.temp_del_celsius
83+
# print(temp.temp_get_celsius)
84+
85+
# If we try and print the attribute, it will give an AttributeError,
86+
# saying that 'TemperatureConvertor' object has no attribute '_celsius'
87+
88+
89+
# Final note in the above example, we didn't use the __init__(), but if your
90+
# code needs to add public attributes you can use the __init__, also you can
91+
# use these attributes with the property function if needed.

0 commit comments

Comments
 (0)