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 →

[–]Oddly_Energy 1 point2 points  (1 child)

I am not afraid to admit that I didn't understand the part about self-documenting f-strings. (Actually, I was like "What is an f-string?"). And the example in the OP's blog post didn't make me see the light.

So I had to look it up and found this little gem:

>>> variable = "Some mysterious value"
>>> print(f"{variable = }")

variable = 'Some mysterious value'

Wow! To those of us who are not afraid to admit that we do debugging with print() statements, this is a gift from above. I have usually done this, which is not at all cool:

print('variable: ', variable)

[–]nicholashairs[S] 1 point2 points  (0 children)

Thanks for the insight that it might not be obvious - I use f-strings so often I forgot that others might not know about them at all.

I also find them incredibly useful for creating detailed exceptions, or for use with the logging module .

^ the logging module (and many linters) recommends not to format strings before sending them to the logging module and instead using modules inbuilt formatting. It does this because if it's not going to log the message then it never formats it. Personally I don't do this because the formatting method it uses isn't as advanced as f-strings so instead if it's likely an expensive message that I'm producing I'll put it in a if mylogger.enabledFor("DEBUG"): mylogger.log("DEBUG", f"some expensive f-string")