The __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.a.py contains
b.py contains
The result is that ‘1’ is printed.
Edit: The
__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 tobuiltinsin python3.