r/ProgrammerHumor • • Aug 26 '20

Python goes brrrr

Post image
59.2k Upvotes

789 comments sorted by

View all comments

Show parent comments

42

u/Moldy_pirate Aug 26 '20

Is there ever a reason to use a “regular” string rather than an f”string?

22

u/thirdegree Violet security clearance Aug 26 '20

Logging, there you want to do like:

logging.info("value1: %s, value2: %s", 1, 2)

This is because the formatting is only done if the log line will actually be emitted. It can be a significant performance boost if you have a lot of logging.

5

u/[deleted] Aug 26 '20

You can always have a lazy evaluation, using str::format, i. e. log("python goes b{0}", 'r'*10). The string would only format if it were to be used.

1

u/mxzf Aug 26 '20

IN that case, couldn't you just use log(f'python goes b{"r"*10}') instead, for a cleaner execution? If it's only formatting on execution, the format string can still handle inline processing.

→ More replies (2)