TLDR: Constant Folding in Python
Date: 2021-01-10 Source: https://arpitbhayani.me/blogs/constant-folding-python
Overview
Explore Python's constant folding - how it optimizes code by evaluating expressions at compile time. Dive into CPython's elegant implementation. Python tries to fold every single constant expression present but there are some cases where even though the expression is constant, but Python chooses not to fold it.
Key Points
- Scope of Constant Folding: Python tries to fold every single constant expression present but there are some cases where even though the expression is constant, but Python chooses not to fold it.
- Every programming language aims to be performant in its niche and achieving superior performance requires a lot of compiler level optimizations.
- One famous optimization technique is Constant Folding where during compile time the engine tries to recognize constant expressions, evaluate them, and replaces the expression with this newly evaluated value, making the runtime leaner.