45

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?

6 Answers 6

53

You can just declare a variable on the module level and use it in the module as a global variable. And then you can also import it to other modules.

#mymodule.py
GLOBAL_VAR = 'Magic String' #or matrix...

def myfunc():
    print(GLOBAL_VAR)

And in other modules:

from mymodule import GLOBAL_VAR
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need 'global GLOBAL_VAR' to read the constant. And you shouldn't be writing to it from in the function anyway (otherwise it's not a constant)
38

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.

3 Comments

What is a "variable constant"?
@Tripp it's not variable constant, it's constant variable. I.e. a variable that cannot be changed. For example 'const int x=3;' in C++ declares a variable x that is equal to 3 and will never be changed.
Ah. I was having trouble parsing the last sentence. I will edit it for clarification.
8

There is no way to declare a constant in Python. You can just use

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:

import file1

print file1.SOME_CONSTANT

Assuming that both files are in the same directory.

2 Comments

what if they are not in the same directory? does one import them in some way?
I like this solution better than using global because less lines of code.
3

An alternate is to import the constant directly from your constants file.

Option 1

globals.py

MY_CONSTANT = "Some constant value"

main.py

from globals import MY_CONSTANT

print(MY_CONSTANT)

Option 2

globals.py

MY_CONSTANT = "Some constant value"

main.py

import globals

print(globals.MY_CONSTANT)

So there are options on whether you're importing a bunch of constants in your file (option 2) is better or just a single constant (I prefer option 1).

1 Comment

this should be the right answer.
2

The issue with all of the above solution is that you can by mistake re-set this global variable defined in global.py or module.py in your current file.

The better solution would be to use a getter like function instead of defining a variable in the global.py or module.py

So in your global.py define

def SOME_CONSTANT():
    return <constant_value>

and use in your local.py as

from global import *
if x == SOME_CONSTANT():
    print('x is equal to global constant and its value is: ', SOME_CONSTANT())

with this work around you will actually have an immutable global constant (a getter function under the hood)

Comments

1

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).

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.

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.

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 types that you can exploit).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.