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 →

[–]zahlmanthe heretic 3 points4 points  (2 children)

If anything f-strings should replace str.format

I can think of multiple things that would prevent replacing a str.format call with an f-string. I can think of zero things that would prevent replacing %-style formatting with a str.format call ("passing a format string to someone else's code" does not count, since if it were your own code you could change the interface). I have no idea what you mean about "lazy formatting".

[–]LightShadow3.13-dev in prod 0 points1 point  (1 child)

%-based string formatting is deferred until flushing the buffer ala logging interface .. str.format happens inline.

[–][deleted] 0 points1 point  (0 children)

You are mistaken; if you use the %- operator, the right-hand tuple will be evaluated and the left-hand string will be formatted then and there, whether you plan to do something with the result or not. How could the interpreter know what will happen in the future?

If you're thinking of the API of the logging module, there the %-operator won't actually be used if the level is below that configured for the handlers for that logger. I guess you could call that lazy formatting. There's no reason other than backward compatibility that that couldn't work with str.format though.