0

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
  • 1,739
  • 2
  • 16
  • 37

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
  • 50,400
  • 22
  • 92
  • 131
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,590
  • 1
  • 4
  • 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
  • 235,058
  • 25
  • 372
  • 444