Python 3.7 logging: f-strings vs %
The documentation says that the logging lib is optimized to use the %s
formatting style. I can't remember where it is mentionned exactly, but I read it a few months ago.
Edit - Found! https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles
Edit2 - (thanks to Robin Nemeth): https://docs.python.org/3/howto/logging.html#optimization
IMO, using %s
in your strings is NOT the most modern approach. Definitely, most developers will prefer to use f-strings because it is more convenient and easy to read (and write).
But, you interestingly find a specific case where you may not want to use an f-string. If you need to avoid automatic call of __str__()
method because of optimisation issue, then it is probably a good enough reason to use %s
instead of f-strings. But, this could also indicate that something may be done in your program to reduce the complexity of __str__()
. Most of the time it shouldn't take so much time or resources to calculate a string representation for an object...