dflook / python-minifier

Transform Python source code into its most compact representation
MIT License
553 stars 40 forks source link

const-folding #91

Open mqyhlkahu opened 1 year ago

mqyhlkahu commented 1 year ago

A possible improvement would be const-folding expressions that would be shorter as a constant value.

Example:

SOME_VARIABLE = (((123 * 456) + 789) // 10) & 0xabcdef

can be transformed into

SOME_VARIABLE=1063

# Note that the Python interpreter will do this at compile-time anyway, so it won't have any effect on the code that is actually run.

>>> import dis
>>> dis.dis("(((123 * 456) + 789) // 10) & 0xabcdef")
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1063)
              4 RETURN_VALUE
dflook commented 12 months ago

Thanks for creating an issue @kotnen, keep any eye out for this in the next release 😄