Python implements unchangeable constants. python implements constants.
For various reasons, Python does not provide const Modifiers like C/C ++/Java. In other words, python does not have constants, at least by the end of 2015. Python programs generally use the conventional form of variable names in upper case to indicate that this is a constant, but this is not a long term.
In fact, Python can save the country by curve to achieve constants.
In Python object-oriented,
object.__setattr__()
This built-in function is automatically called when assigning values to class attributes. Its function prototype is:
object.__setattr__(self, name, value)
Here, name is the variable name and value is the variable value.
Object. _ dict _ stores all writable attributes in the object in the form of dict. The key is the variable name, and the value is the variable value.
Then we may create a const class for its object. the _ setattr _ () method overwrite is used to determine the value of the attribute value. If the attribute exists, it indicates that the constant is revalued and an exception is thrown, if the attribute does not exist, it indicates a new constant. You can assign values.
The const. py code is as follows:
# -*- coding: utf-8 -*-class _const: class ConstError(TypeError) : passdef __setattr__(self, key, value): # self.__dict__ if self.__dict__.has_key(key): raise self.ConstError,"constant reassignment error!" self.__dict__[key] = valueimport syssys.modules[__name__] = _const()
Line 1-10 is an implementation of the class above.
Lines 12-14 are worth noting. Although we have the _ const class, we still need
import constc = const._const()c.TEST_CONSTANT = 'test'
Declare a constant TEST_CONSTANT in this form. However, we want to assign values to constants in a more concise way. Shape:
import constconst.TEST_CONSTANT = 'test'
In python, The __name _ built-in attribute is the value of the current class or type. In general, the value of __name _ has the following two forms:
- If you run a py file, the value of __name _ in this file is '_ main __'
- If a py file is imported, the value of __name _ in the file to be imported is the file name of the file (without the. py suffix)
Sys. modules is a dict object, including information about all modules loaded by python in the current context. The key of dict is the file name, and value is the module object.
In const. py, 14 lines are equivalent
import constsys.modules['const'] = _const()
That is, let the _ const class serve as the entry point of the module, and the introduction of const. py is equivalent to declaring an instance of the _ const class.
So far, the python constant has been implemented and tested using test. py:
# -*- coding: utf-8 -*-import constconst.TEST = 'test'print const.TESTconst.TEST1 = 'test1'print const.TEST1const.TEST = 'test'print const.TEST
The printed information is as follows:
testtest1Traceback (most recent call last): File "H:/code/test.py", line 9, in <module> const.TEST = 'test' File "H:\code\const.py", line 9, in __setattr__ raise self.ConstError,"constant reassigning error!"const.ConstError: constant reassignment error!
The value is successfully assigned to the two constants, and an exception is thrown when you try to modify the first constant value :)