Let’s be very specific about what this means. Usually, compile-time directives are literally equivalent to cutting pieces of the code out, which allows you to have directives managing partial syntax, like this:
#if COLLECT_RESULTS
#define COMMA ,
results = [
#else
#define COMMA
#endif
thing_one() COMMA
thing_two() COMMA
thing_three() COMMA
thing_four() COMMA
#if COLLECT_RESULTS
]
#endif
This is entirely legal in languages like C, where the preprocessor examines the text before the compiler looks at it (hence “pre” processing). This isn’t how it works with __debug__, which is simply a magic constant. You can’t do something like:
if __debug__:
stuff = [
# then this part is unconditional
thing_one()
if __debug__:
]
There is a strict flow through compilation: first preprocessing, then syntax parsing, and then optimization happens afterwards. The fact that you can’t see the debug code in disassembly does NOT mean that it got deleted in preprocessing; it means it got optimized out. (You can often see some residue of it, with a NOP opcode left behind.)
So, when you ask for compile-time directives, do you want something that can manipulate syntax, or are you simply looking for something that gets evaluated during the compilation phase and then is treated as a constant? Those are two very different requirements, with correspondingly different ideas to fulfil them.
For something that can manipulate syntax, I would recommend getting an actual preprocessor. You can do neat stuff with import hooks, or for simplicity, just run your code through m4 or similar as part of deployment. But for a compile-time constant? That would require some support from the Python language. It would be a narrower proposal than a full preprocessor, but OTOH would also have broader application. Imagine if you had something like this:
def f():
if $(math.random() < 0.5):
print("I'm nondeterministic!")
else:
print("Why are you doing something insane?")
I’m using the deliberately-ugly $(EXPR) syntax as a placeholder here. If this meant “evaluate this once, while the module is being compiled, and put the result here as a constant”, it could be used for all kinds of things - it’s basically constant folding, but for arbitrary expressions. And since it has effectively become a constant, the optimizer could then kick in afterwards, treating this as either if True: or if False: depending on which way the RNG decided.
Which one was it that you had in mind?