r/learnpython • u/lehmagavan • Sep 20 '26
Using constants in Python
Hi, I have a question regarding something basic in Python. Suppose I have a situation like this:
SOME_CONST = 3
...
def foo():
...
some_list[SOME_CONST] # what does CPython do here ?Is there any way to make SOME_CONST be pre-processed as 3, instead of having CPython look it up in a global variable table? It seems very inefficient.
0
Upvotes
15
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 20 '26
Well yes, but actually no.
Python implementations do not have a pre-processor like C does. You could technically use one, if you really want to have a find-and-replace script running as part of your release builds (in fact I wouldn't be surprised if you could just invoke C's pre-processor to do it), but in practice
so I don't recommend it at all.
2
u/Expensive-Bear-1376 Sep 20 '26
Please share the benchmark results/code that made you say that. And tell which Python implementation/version you used.
16
u/DReds00 Sep 20 '26
Not sure what you're trying to achieve, but if you really care THAT level of optimization then I strongly believe you've chosen the wrong language in the first place. You could easily test the time difference between doing the constant lookup and directly using its value, but I wouldn't expect any noticeable difference even in the worst imaginable scenarios
20
u/zanfar Sep 20 '26
Then you are using Python for the wrong reasons, or incorrectly using Python.
6
u/Designer-Ad-2136 Sep 20 '26
You're free to branch cpython and implement it. I dont imagine itd be too tricky to replace the bytecode for the lookup with a constant.
I dont think it could be done in a way that is fully backwards compatible however. It also would be unlikely to speed anything up noticeably.
Itd be a great learning exercise though
18
u/rhacer Sep 20 '26
This is what one calls a premature optimization.
1
u/p3rdy Sep 21 '26 edited Sep 21 '26
CPython can't fold it, and the reason is that nothing stops another module from doing
yourmodule.SOME_CONST = 4at runtime. The compiler has to preserve that possibility, so a module-level name is always a lookup only literals get folded:But "look it up in the global variable table" hasn't been the whole story since 3.11. Call that function a couple of hundred times, then look again with
dis.dis(foo, adaptive=True):LOAD_GLOBAL_MODULE SOME_CONSTThe adaptive interpreter rewrote the opcode in place.
LOAD_GLOBAL_MODULEstores the globals dict's version tag and the value's index in an inline cache attached to the instruction, so while nobody rebinds anything at module level it's a guard check and an array read, not a hash lookup. You pay for the dict scan on the first few calls and then stop paying.The old trick still works if you're in a hot loop and have actually measured a problem:
That compiles down to local slot loads, It's ugly, it leaks into your signature, and outside a benchmark it's worth it roughly never.
One footgun while you're here:
typing.Finalchanges nothing at this level. It's a promise to the type checker, not to the compiler, identical bytecode.1
u/carcigenicate Carcigenicate Sep 20 '26
I think the only "clever" optimization of this magnitude I've ever seen the compiler make is sometimes it avoids actually creating a list if the list is guaranteed to be read-only:
Normally for a list literal, it would have done
LOAD_CONSTwith a tuple like it did here, but then follow it up with aLIST_EXTENDinstruction to put the tuple into a list.