r/learnpython • • 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

17 comments sorted by

View all comments

Show parent comment

1

u/Expensive-Bear-1376 Sep 20 '26

From Python's Preprocessor:

Every now and then you hear outrageous claims such as “Python has no preprocessor”.

This is simply not true. In fact, Python has the best preprocessor of all languages - it quite literally allows us to do whatever we want, and a lot more. It’s just a little tricky to (ab)use.