<p>The <code>__debug__</code> 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?</p>

<p>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.</p>

This sounds like modifying the
```
__builtin__
```
name space.  To do it:
```
```
import __builtin__
__builtin__.foo = 'some-value'
```
```
Do not use the
```
__builtins__
```
directly (notice the extra "s") - apparently this can be a dictionary or a module.  Thanks to ΤΖΩΤΖΙΟΥ for pointing this out, more can be found
here
.
Now
```
foo
```
is available for use everywhere.
I don't recommend doing this generally, but the use of this is up to the programmer.
Assigning to it must be done as above, just setting
```
foo = 'some-other-value'
```
will only set it in the current namespace.