I was looking through code like this: foo = '%s%s%s' % ('https://', host, uri) and realized this could be rewritten by the interpreter as: foo = 'https://%s%s' % (host, uri) I tried to determine how much code this might affect, but it was pretty hard for me to come up with a decent regex to filter out all the false positives. There were too many hits to determine if this would be used often.
> and realized this could be rewritten by the interpreter as: Yeah, it could but it's tricky to implement it. The current peephole is implemented in C. You may first try to implement it using my astoptimizer project which is implemented in Python. At least to check if it's possible or not :-) https://bitbucket.org/haypo/astoptimizer astoptimizer only optimizes str%args if it succeed at compile time, so if all arguments are constant and no error is raised.
I think this operation is more suitable for AST optimizer then peephole but i'm still not sure if such a conversion gains anything compared to usage of old type string formattings.
Am thinking this should be closed. It isn't clear that it can be done easily or that there would be a measurable impact. Also, the advent of f-strings means that code like this will occur much less often -- both code examples presented would likely no longer be coded as they once were.