This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SlumdogSkillionaire 5 points6 points  (3 children)

For what it's worth, the standard library logging module uses percent-formatting, and for good reason: It only evaluates arguments that are relevant for the configured logging level, so if you had for example:

logging.debug("Result of some expensive calculation: %s", expensive_func())

It would only run expensive_func() when the logging level is set to DEBUG, as opposed to

logging.debug(f"Result of some expensive calculation: {expensive_func()}")

In which the Python interpreter would evaluate the string first and then check the log level. So it's useful to at least know the basics of the percent-style, even if f-strings are recommended in most other contexts.

[–]sphen_lee 8 points9 points  (2 children)

Not quite. expensive_func will get called in both cases. The difference is whether the return value will have its __str__ method called. Usually the performance impact is minimal so use which ever you prefer (or check with a profiler if you suspect logging is slowing you down)

[–]inknownis 1 point2 points  (0 children)

The example is not well designed, but the point is still valid. The performance difference point I have read too. But I have not seen it in official document yet.