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.
Asked
Active
Viewed 589 times
1
-
https://stackoverflow.com/a/69796852/2318649 – DisappointedByUnaccountableMod Nov 05 '21 at 14:53
-
5If the string you want to format is a variable, f-strings aren't even an option – user2390182 Nov 05 '21 at 14:54
3 Answers
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