PY
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
# 5️⃣ methods of making strict constants https://realpython.com/python-constants
#1️⃣
class ConstantsNamespace:
__slots__ = () # Guarantee that no one else changes the constants.
PI = 3.141592653589793
try:
constants = ConstantsNamespace()
print(constants.PI)
constants.PI = 3.14
except AttributeError:
print('1️⃣ Can’t assign new value')
#2️⃣
class ConstantsNamespace:
@property
def PI(self):
return 3.141592653589793
try:
constants = ConstantsNamespace()
print(constants.PI)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run