Re:Python 3's F-Strings: An Improved String Formatting Syntax
by dagenix on Wednesday January 20, 2021 @11:09PM
(#25855280)
As the sibling comment points out, it actually doesn't work that way. The first parameter is only treated as a format string _if_ there are arguments. If not, it is treated as a literal string: https://github.com/python/cpython/blob/dcea78ff53d02733ac598...
Re:Python 3's F-Strings: An Improved String Formatting Syntax
by ploxiln on Thursday January 21, 2021 @12:23AM
(#25855754)
I stand corrected. Thanks.
(That's what I get for assuming consistent behavior :P)
Re:Python 3's F-Strings: An Improved String Formatting Syntax
by dagenix on Thursday January 21, 2021 @01:32AM
(#25856157)
I only found out about this quirk a few months ago immediately after I had given the same warning you had to someone else. It's extremely unintuitive.
Re:Python 3's F-Strings: An Improved String Formatting Syntax
by altncatchfire on Wednesday January 20, 2021 @10:55PM
(#25855204)
I dont think this is true. I just tried with the following program and it runs as expected (Run on Python 3.7.9).
import logging
got = "%d"
logging.warning(f"hi {got}")
logging.debug(f"hi {got}")
Output WARNING:root:hi %d
DEBUG:root:hi %d
(Also, for the logging functions, you don't pass a tuple, just a normal series of arguments.)