
Hi Phillip, thanks for your interest in CPython. How Python views your code isn't how you view your code. CPython views source code instead as something called "bytecode/wordcode". This bytecode is a lower-level intermediary language that the CPython interpreter executes. You can read more about bytecode at the documentation for the dis module [1]. Operations are thus viewed at the bytecode level. The inline caching is done on a per-bytecode basis. A complicated program would be split into many bytecode that each does a smaller operation relative to the larger program. This is why our guards when using information from the inline cache are very simple, because the operations themselves are relatively simpler to something as big as a function. This is also how we can ensure that side effects in our operations don't break anything.
I actually find myself often factoring such data out of loops in Python, whereas in C I would just leave that to the optimizer/compiler.
The compiler in CPython can't really do that because it's not safe in Python. The user could've overridden `__add__` to do more things than just addition. [1] https://docs.python.org/3/library/dis.html