cjm2671
cjm2671

Reputation: 19456

How do you declare a global constant in Python?

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?

Upvotes: 43

Views: 127633

Answers (6)

Anuj Sethi
Anuj Sethi

Reputation: 662

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)

Upvotes: 1

M&#225;ty&#225;s Kuti
M&#225;ty&#225;s Kuti

Reputation: 820

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

Upvotes: 51

wired00
wired00

Reputation: 14438

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

Upvotes: 3

SpKel
SpKel

Reputation: 542

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.

Upvotes: 37

Burhan Khalid
Burhan Khalid

Reputation: 174614

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

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

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.

Upvotes: 8

Related Questions