Data
Constant
Python
def constant(f):
def fset(self, value):
raise TypeError
def fget(self):
return f()
return property(fget, fset)
class constants(object):
@constant
def PI():
return 3.14
@constant
def E():
_CONST = constants()
return (_CONST.PI**4 + _CONST.PI**5)**(1.0/6)
CONST = constants()
There is no native support for Constants in Python.
Here we are mimicking the constant behavior by defining getters and throwing an error on setters access.
print(CONST.PI) # 3.14
print(CONST.E) # 2.7171888402596216
CONST.PI = 2.12 # Error