• notice
  • Congratulations on the launch of the Sought Tech site

Python global constants are alternative implementation methods for cross-file global variables

    Constants are variables that do not change. In Python, constants are usually represented by uppercase variable names. But in fact, Python does not have a strict concept of constants. Python has built-in constants, and there are only 6 of them, namely True, False, None, NotImplemented, Ellipsis, __debug__. There is no direct way or command to declare a global constant in Python. There is not even a definition of the global constant data type in python.

Python's search order for variable keywords is: local namespace -> global namespace -> built-in namespace. If the variable name cannot be found, it will give up and raise a NameError exception. in

Built-in names (built-in names) are built-in names in the Python language, such as function names abs, char and exception names BaseException, Exception and so on.
Global names (global names) are the names defined in the module, which record the variables of the module, including functions, classes, other imported modules, module-level variables and constants.

Local names (local names) are the names defined in the function, which record the variables of the function, including the parameters of the function and the locally defined variables. (also defined in the class)

    It can be seen that the global name in Python is the name defined in the module. The variables defined inside the function have a local scope, and the variables defined outside the function have the global scope. And the global scope here is only inside a module. Therefore, to realize the global constant of the whole framework, there are two ways to realize it. One is to define directly in the built-in scope, and the other is to create a constant value module, so wherever constants need to be used, import this module and perform operations of fetching and setting values, so as to simulate the function of implementing global constants. So the global constant below is actually a global variable across files and modules.

    The built-in scope is implemented through a standard module called builtin, but the variable name itself is not put into the built-in scope, so this file must be imported to be able to use it. In Python3.0, you can use the following code to see which variables are predefined:
import builtins

    However, there is no way to modify and add built-in variables. To create a constant value module, you can use an existing module such as sys, or build a module yourself.

The implementation using the sys module is as follows:

# Display the values under all sys modules
import sys
# Set up a custom constant data pool
# It can be called directly globally
print(sys. modules['const_vals'])

   The method of using a custom module to implement the constant value module is as follows: You can create a const.py to define a Const class, the content is as follows:

_global_dict = {}
class Const:
def _init():
#Initialize a global dictionary

def set_value(key,value):
_global_dict[key] = value

def get_value(key,value):
except KeyError as e:

Introduce this module in other places that need to use global constants to store and fetch values.


Technical otaku

Sought technology together

Related Topic


Leave a Reply