For a variety of reasons, Python does not provide a const modifier like C/c++/java, in other words, there is no constant in Python, at least until the end of 2015, and there is no such plan. Python programs generally express this as a constant through the use of a conventional variable name in full capitalization, but this is not a permanent one.
In fact, Python can achieve the constant curve of salvation.
In the object-oriented python,
Object. __setattr__ ()
This built-in function is called automatically when assigning values to the properties of a class. Its function prototypes are:
Object. __setattr__ (Self, name, value)
Where name is the variable name, and value is the variable value.
object.__dict__, in the form of dict, saves all writable attributes in the object, key is the variable name, and value is the variable.
Then it is possible for us to establish a const class to overwrite its object.__setattr__ () method, and to judge when the property value is assigned, if the property exists, it is a re-assignment of the constant, which throws an exception, if the property does not exist, Indicates that a constant is newly declared and can be assigned an operation.
The const.py code is as follows:
#-*-coding:utf-8-*-class_const:classConsterror (TypeError):Passdef __setattr__(self, Key, value):#self.__dict__ ifSelf.__dict__. Has_key (key):RaiseSelf. Consterror,"constant reassignment error!"Self .__dict__[Key] =valueImportsyssys.modules[__name__] = _const ()
Of these, 1-10 lines is an implementation of the class of the above-mentioned ideas.
The wording of the 第12-14 line is worth explaining. Although we have the _const class, we still need to use this class
Import='test'
This form declares a constant test_constant, but we want to use a more concise method to assign a constant value. Shaped like:
Import'test'
In Python, the __name__ built-in property is the value of the current class or type. In layman's words, the value of __name__ has the following two forms:
- If you run a py file, the value of __name__ in the file is ' __main__ '
- If you import a py file, in the import file, the value of __name__ is the file name of the file (without the. py suffix)
The Sys.modules is a Dict object that includes information about all the modules in the current context where Python has been load, Dict key is the file name, and value is the module object.
In const.py, the 14-line notation is equivalent to
Import constsys.modules['const'] = _const ()
That is, having the _const class as the entry point for the module, introducing const.py is equivalent to declaring an instance of a _const class.
Now that the Python constants are complete, use the test.py test:
# -*-coding:utf-8-*- Import 'test'print'test1 ' Print'test'print Const. TEST
Print the following information:
Testtest1traceback (most recent): File "h:/code/test.py", line 9, <module> Const. Test = ' test ' File "H:\code\const.py", line 9, in __setattr__ raise self. Consterror, "Constant reassigning error!" Const. Consterror:constant reassignment error!
A successful assignment of two constants throws an exception when attempting to modify the first constant value:)
Python implements non-modifiable constants