你如何在 Python 中声明一个全局常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24133929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you declare a global constant in Python?
提问by cjm2671
I have some heavy calculations that I want to do when my program starts, and then I want to save the result (a big bumpy matrix) in memory so that I can use it again and again. My program contains multiple files and classes, and I would like to be able to access this variable from anywhere, and if possible define it as constant.
当我的程序启动时,我想进行一些繁重的计算,然后我想将结果(一个很大的颠簸矩阵)保存在内存中,以便我可以一次又一次地使用它。我的程序包含多个文件和类,我希望能够从任何地方访问这个变量,如果可能的话,将它定义为常量。
How do you define a global constant in Python?
你如何在 Python 中定义全局常量?
采纳答案by Mátyás Kuti
You can just declare a variable on the module level and use it in the module as a global
variable. An you can also import it to other modules.
您可以在模块级别声明一个变量并在模块中将其用作global
变量。您也可以将其导入其他模块。
#mymodule.py
GLOBAL_VAR = 'Magic String' #or matrix...
def myfunc():
global GLOBAL_VAR
#do something
Or in other modules:
或者在其他模块中:
from mymodule import GLOBAL_VAR
回答by Christian
There is no way to declare a constant in Python. You can just use
在 Python 中无法声明常量。你可以使用
SOME_CONSTANT = [...]
If the file name where it is declared is file1.py
, then you can access to it from other files in the following way:
如果声明它的文件名是file1.py
,那么您可以通过以下方式从其他文件访问它:
import file1
print file1.SOME_CONSTANT
Assuming that both files are in the same directory.
假设两个文件都在同一个目录下。
回答by Burhan Khalid
I am not sure what you mean by 'global constant'; because there are no constants in Python (there is no "data protection", all variables are accessible).
我不确定您所说的“全局常量”是什么意思;因为 Python 中没有常量(没有“数据保护”,所有变量都可以访问)。
You can implement a singleton pattern, but you will have to regenerate this at runtime each time.
您可以实现单例模式,但每次都必须在运行时重新生成它。
Your other option will be to store the results in an external store (like say, redis) which is accessible from all processes.
您的另一个选择是将结果存储在可从所有进程访问的外部存储(例如 redis)中。
Depending on how big your data set is, storing it externally in a fast K/V like redis might offer a performance boost as well.
根据您的数据集有多大,将其存储在像 redis 这样的快速 K/V 外部也可能会提供性能提升。
You would still have to transform and load it though, since redis would not know what a numpy array is (although it has many complex typesthat you can exploit).
回答by SpKel
I do not think the marked as good answer solves the op question. The global
keyword in Python is used to modify a global variable in a local context (as explained here). This means that if the op modifiesSOME_CONSTANT
within myfunc
the change will affect also outsidethe function scope (globally).
我认为标记为好的答案不能解决 op 问题。global
Python 中的关键字用于修改局部上下文中的全局变量(如此处所述)。这意味着如果操作在更改SOME_CONSTANT
内myfunc
修改也会影响函数范围之外(全局)。
Not using the global
keyword at the begining of myfunc
is closer to the sense of global constantthan the one suggested. Despite there are no means to make a variable constant in Python.
global
在 of 开头不使用关键字比建议的myfunc
更接近全局常量的意义。尽管没有办法在 Python 中使变量成为常量。