1

I personally couldn't think of anything, but I'm wondering if there are some edge cases, where an older method might be somehow better.

fbence
  • 2,025
  • 2
  • 19
  • 42

3 Answers3

3

It is often better to do logging calls via

log.info("some log {} data {} to be logged", arg1, arg2)
# will be `message.format(*args)` at the end of the day

vs.

log.info(f"some log {arg1} data {arg2} to be logged")

The reason is that if my logger is not configured to log INFO logs then the the second snippet does a potentially expensive string interpolation, converts the arguments to strings, etc. The first snippet does not do the interpolation and returns early without serializing the arguments.

luk2302
  • 55,258
  • 23
  • 97
  • 137
2

Yes, when you need to reuse the same template string in multiple ways is better to use formatted strings. For example take a look at this question

Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
2

f-strings aren't an option for anything you want to be localizable. An f-string is hard-coded directly into your program's source code, so you can't dynamically swap it out for a translated template string based on the user's language settings.

user2357112
  • 260,549
  • 28
  • 431
  • 505