3 ms·
F-strings are great. The only downside I’ve found is they are incompatible with the stdlib logging framework’s deferred string interpolation feature (using %s s
by theptip 6y ago
F-strings are great. The only downside I’ve found is they are incompatible with the stdlib logging framework’s deferred string interpolation feature (using %s style string interpolation), which can give you a nice performance boost if you make lots of expensive debug logs for tests, but run at info level in production.
That said most apps probably won’t see much of a difference in perf between the two approaches.
- ericvsmith 6y agof-strings are so much faster that I've seen an increase in speed over %-formatting for logging. If there's even a 10% chance that a string will be actually logged (and therefore %-formatting will take place), I'll replace it with an f-string and not care about the deferred formatting.
- pfranz 6y agoHere are some benchmarks and a discussion about using f-strings in logging I remember reading a few years ago: https://github.com/PyCQA/pylint/issues/2395 https://github.com/PyCQA/pylint/issues/2395