Globals are bad. But sometimes they are a far simpler and more expedient solution than the alternative.
As a guard against inadvertent changes to globals, this is what I've been using for some time.
I'd make a separate .py, for instance myGlobalVariables.py and within it I'd have code like:
def setGlobalPrimes(primesIn):
global globalPrimes
globalPrimes = primesIn
Then, from within my main code, I'd
import myGlobalVariables
def foo(): #foo that runs 5000 times
primes = myGlobalVariables.globalPrimes
#calcs & stuff here
return answers
if __name__ == "__main__":
#prime all globals
myGlobalVariables.setGlobalPrimes(sorted([x for x in range(1, 10000) if is_prime(x)]))
#Then whatever you have in your code (obviously below is just me being silly)
for i in range(0, 5000):
foo()
Basically, it makes the chances of you inadvertently updating/overwriting a global much harder. You can of course do it by:
myGlobalVariables.globalPrimes = [blah blah]
It stops you doing so many re-calcs, thus speeding up your code a bit.