constant folding - why not more

Serhiy Storchaka storchaka at gmail.com
Wed Nov 11 01:59:27 EST 2020


11.11.20 02:46, Skip Montanaro пише:
> I can think of two reasons. One, this kind of comparison will almost never
> appear in production code (maybe in unit tests?). Unlike the C family of
> languages, Python doesn't have a macro processor which would give symbolic
> names to numeric constants or string literals. Code generators might
> conceivably generate constant comparisons, but they might be able to easily
> do constant folding of comparisons themselves.
> 
> Two, given that this sort of construct will almost never be found in the
> wild, folding constant comparisons in the compiler would increase the
> maintenance burden of the compiler (just slightly, but still...) with no
> clear benefit.

I concur with Skip. Expressions like 2**32-1 or 1/3 or b'A'[0] are
pretty common, so it makes sense to evaluate them at compile time. But
comparison and boolean operators are newer used with constants in
production code.



More information about the Python-list mailing list