3 ms·
Also, remember that the first argument to the logging functions is considered to be a format string. So using f-strings is not just less efficient, it is also n
by ploxiln 6y ago
Also, remember that the first argument to the logging functions is considered to be a format string. So using f-strings is not just less efficient, it is also not robust, unless you do:
logging.debug("%s", f"Unexpected, got {got}")
because otherwise, if the format string is the first/only argument, and `got` unexpectedly contain `%`, an exception will be raised due to a missing format parameter.
(Also, for the logging functions, you don't pass a tuple, just a normal series of arguments.)
- altncatchfire 6y agoI 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
- dagenix 6y agoAs 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/dcea78ff53d02733ac5983255610b651aa1c0034/Lib/logging/__init__.py#L363 https://github.com/python/cpython/blob/dcea78ff53d02733ac598...