Constants in Python
Constants in Python
Code Explanation:
1.
Define a function constant that takes an expression, and uses it to construct
a "getter" - a function that solely returns the value of the expression.
2.
The setter function raises a SyntaxError so it's read-only
3.
CONST = _Const()
print CONST.FOO
##3131964110
CONST.FOO = 0
##Traceback (most recent call last):
## ...
## CONST.FOO = 0
##SyntaxError: None
Then use the built-in property function to construct an object that can be
"set" or "get".
3.
Note hat the property function's first two parameters are
named fset and fget.
4.
Use the fact that we chose these very names for our own getter & setter and
create a keyword-dictionary using the ** (double asterisk) applied to all the
local definitions of that scope to pass parameters to the property function
If you want that this constant never changes, you can hook into attribute access
and do tricks, but a simpler approach is to declare a function
def MY_CONSTANT():
return "one"