How do you declare a global constant in Python?
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 modifies SOME_CONSTANT
within myfunc
the change will affect also outside the function scope (globally).
Not using the global
keyword at the begining of myfunc
is closer to the sense of global constant than the one suggested. Despite there are no means to render a value constant or immutable in Python.
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.
#mymodule.py
GLOBAL_VAR = 'Magic String' #or matrix...
def myfunc():
print(GLOBAL_VAR)
Or in other modules:
from mymodule import GLOBAL_VAR