__debug__ variable is handy in part because it affects every module. If I want to create another variable that works the same way, how would I do it?
The variable (let's be original and call it 'foo') doesn't have to be truly global, in the sense that if I change foo in one module, it is updated in others. I'd be fine if I could set foo before importing other modules and then they would see the same value for it.
I don't endorse this solution in any way, shape or form. But if you add a variable to the
__builtin__ module, it will be accessible as if a global from any other module that includes
__builtin__ -- which is all of them, by default.
import __builtin__ __builtin__.foo = 1 import a
The result is that "1" is printed.
__builtin__ module is available as the local symbol
__builtins__ -- that's the reason for the discrepancy between two of these answers. Also note that
__builtin__ has been renamed to
builtins in python3.
If you need a global cross-module variable maybe just simple global module-level variable will suffice.
var = 1
import a print a.var import c print a.var
import a a.var = 2
$ python b.py # -> 1 2