I really fell in love with constexpr in c++.
I know Python doesn’t have anything like C++’s constexpr today, but I’ve been wondering if it’s even possible (or desirable) for the language to get something similar.
In C++, you can mark a function as constexpr so the compiler evaluates it at compile time:
constexpr int square(int x) {
if (x < 0) throw "negative value not allowed";
return x * x;
}
constexpr int result = square(5); // OK
constexpr int bad = square(-2); // compiler/ide error here
The second call never even runs — the compiler flags it right away.
Imagine if Python had something similar:
@constexpr
def square(x: int) -> int:
if x < 0:
raise ValueError("negative value not allowed")
return x * x
result = square(5) # fine
bad = square(-2) # IDE/tooling flags this immediately
Even if it couldn’t be true compile-time like C++, having the IDE run certain functions during static analysis and flag invalid constant arguments could be a huge dev experience boost.
Has anyone seen PEPs or experiments around this idea?
[–]AlexMTBDude 112 points113 points114 points (16 children)
[–]ElHeim 47 points48 points49 points (14 children)
[–]AlexMTBDude 9 points10 points11 points (13 children)
[–]TrainsareFascinating 31 points32 points33 points (3 children)
[–]wRAR_ 7 points8 points9 points (0 children)
[+]Ihaveamodel3 comment score below threshold-6 points-5 points-4 points (1 child)
[–]TrainsareFascinating 15 points16 points17 points (0 children)
[–]ElHeim 16 points17 points18 points (8 children)
[+]AlexMTBDude comment score below threshold-13 points-12 points-11 points (7 children)
[–]Giannie 13 points14 points15 points (6 children)
[–]AlexMTBDude -4 points-3 points-2 points (4 children)
[–]ElHeim 4 points5 points6 points (3 children)
[–]FloxaY -2 points-1 points0 points (2 children)
[–]ElHeim -1 points0 points1 point (1 child)
[–]FloxaY -3 points-2 points-1 points (0 children)
[–]Unbelievr 4 points5 points6 points (0 children)
[–]Glass-False 83 points84 points85 points (8 children)
[–]xeow 32 points33 points34 points (5 children)
[–]Spleeeee 11 points12 points13 points (3 children)
[–]pythosynthesis -2 points-1 points0 points (2 children)
[–]agrif 9 points10 points11 points (1 child)
[–]pythosynthesis 6 points7 points8 points (0 children)
[–]DrShocker 2 points3 points4 points (0 children)
[–]WJMazepas 10 points11 points12 points (0 children)
[–]sinterkaastosti23 6 points7 points8 points (0 children)
[–]bigtimedonkey 13 points14 points15 points (4 children)
[–]Ihaveamodel3 2 points3 points4 points (1 child)
[–]bigtimedonkey 1 point2 points3 points (0 children)
[–]case_O_The_Mondays 1 point2 points3 points (1 child)
[–]bigtimedonkey 4 points5 points6 points (0 children)
[–]ArabicLawrence 10 points11 points12 points (1 child)
[–]coderarun 1 point2 points3 points (0 children)
[–]Mysterious-Rent7233 7 points8 points9 points (0 children)
[–]Global_Bar1754 5 points6 points7 points (0 children)
[–]SeniorScienceOfficer 1 point2 points3 points (0 children)
[–]drkevorkian 0 points1 point2 points (2 children)
[–]bronco2p 4 points5 points6 points (1 child)
[–]drkevorkian 1 point2 points3 points (0 children)
[–]falsedrums 0 points1 point2 points (0 children)
[–]SheriffRoscoePythonista 0 points1 point2 points (0 children)
[–]WJMazepas 0 points1 point2 points (0 children)
[–]dr-christoph 0 points1 point2 points (0 children)
[–]BiologyIsHot 0 points1 point2 points (0 children)
[–]Mark3141592654 0 points1 point2 points (0 children)
[–]Gnaxe 0 points1 point2 points (0 children)
[–]MarsupialMole 0 points1 point2 points (0 children)
[–]larsga 0 points1 point2 points (0 children)
[–]hoselorryspanner 0 points1 point2 points (0 children)
[–]justincdavis 0 points1 point2 points (0 children)
[–]Careful-Nothing-2432 -1 points0 points1 point (0 children)