<i id='8Knd1'><tr id='8Knd1'><dt id='8Knd1'><q id='8Knd1'><span id='8Knd1'><b id='8Knd1'><form id='8Knd1'><ins id='8Knd1'></ins><ul id='8Knd1'></ul><sub id='8Knd1'></sub></form><legend id='8Knd1'></legend><bdo id='8Knd1'><pre id='8Knd1'><center id='8Knd1'></center></pre></bdo></b><th id='8Knd1'></th></span></q></dt></tr></i><div id='8Knd1'><tfoot id='8Knd1'></tfoot><dl id='8Knd1'><fieldset id='8Knd1'></fieldset></dl></div>
<tfoot id='8Knd1'></tfoot>
<legend id='8Knd1'><style id='8Knd1'><dir id='8Knd1'><q id='8Knd1'></q></dir></style></legend>
    • <bdo id='8Knd1'></bdo><ul id='8Knd1'></ul>

    <small id='8Knd1'></small><noframes id='8Knd1'>

        PyLint 消息:记录格式插值

        时间:2024-04-20

          <tfoot id='nuTFu'></tfoot>

              <i id='nuTFu'><tr id='nuTFu'><dt id='nuTFu'><q id='nuTFu'><span id='nuTFu'><b id='nuTFu'><form id='nuTFu'><ins id='nuTFu'></ins><ul id='nuTFu'></ul><sub id='nuTFu'></sub></form><legend id='nuTFu'></legend><bdo id='nuTFu'><pre id='nuTFu'><center id='nuTFu'></center></pre></bdo></b><th id='nuTFu'></th></span></q></dt></tr></i><div id='nuTFu'><tfoot id='nuTFu'></tfoot><dl id='nuTFu'><fieldset id='nuTFu'></fieldset></dl></div>
              • <bdo id='nuTFu'></bdo><ul id='nuTFu'></ul>
                <legend id='nuTFu'><style id='nuTFu'><dir id='nuTFu'><q id='nuTFu'></q></dir></style></legend>
                  <tbody id='nuTFu'></tbody>

                  <small id='nuTFu'></small><noframes id='nuTFu'>

                1. 本文介绍了PyLint 消息:记录格式插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对于以下代码:

                  logger.debug('message: {}'.format('test'))
                  

                  pylint 产生以下警告:

                  记录格式插值 (W1202):

                  在日志记录函数中使用 % 格式并将 % 参数传递为参数 当日志语句的调用形式为记录.(format_string.format(format_args ...))".这样的调用应改用 % 格式,但将插值留给通过将参数作为参数传递来记录函数.

                  Use % formatting in logging functions and pass the % parameters as arguments Used when a logging statement has a call form of "logging.(format_string.format(format_args...))". Such calls should use % formatting instead, but leave interpolation to the logging function by passing the parameters as arguments.

                  我知道我可以关闭此警告,但我想了解它.我假设使用 format() 是在 Python 3 中打印语句的首选方式.为什么对于 logger 语句不适用?

                  I know I can turn off this warning, but I'd like to understand it. I assumed using format() is the preferred way to print out statements in Python 3. Why is this not true for logger statements?

                  推荐答案

                  logger 语句不是这样,因为它依赖于以前的 "%" 格式(如字符串),使用给予 logger 调用的额外参数来提供此字符串的惰性插值.例如,不要这样做:

                  It is not true for logger statement because it relies on former "%" format like string to provide lazy interpolation of this string using extra arguments given to the logger call. For instance instead of doing:

                  logger.error('oops caused by %s' % exc)
                  

                  你应该这样做

                  logger.error('oops caused by %s', exc)
                  

                  所以只有在实际发出消息时才会对字符串进行插值.

                  so the string will only be interpolated if the message is actually emitted.

                  使用 .format() 时,您无法享受此功能.

                  You can't benefit of this functionality when using .format().

                  根据 logging 文档的 优化 部分:

                  Per the Optimization section of the logging docs:

                  消息参数的格式被推迟到无法避免为止.但是,计算传递给 logging 方法的参数也可能很昂贵,如果 logger 只是丢弃您的事件,您可能希望避免这样做.

                  Formatting of message arguments is deferred until it cannot be avoided. However, computing the arguments passed to the logging method can also be expensive, and you may want to avoid doing it if the logger will just throw away your event.

                  这篇关于PyLint 消息:记录格式插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何配置 Atom 以运行 Python3 脚本? 下一篇:如何指定用于创建虚拟环境的 python 版本?

                  相关文章

                      • <bdo id='p6BEd'></bdo><ul id='p6BEd'></ul>

                    1. <small id='p6BEd'></small><noframes id='p6BEd'>

                      <i id='p6BEd'><tr id='p6BEd'><dt id='p6BEd'><q id='p6BEd'><span id='p6BEd'><b id='p6BEd'><form id='p6BEd'><ins id='p6BEd'></ins><ul id='p6BEd'></ul><sub id='p6BEd'></sub></form><legend id='p6BEd'></legend><bdo id='p6BEd'><pre id='p6BEd'><center id='p6BEd'></center></pre></bdo></b><th id='p6BEd'></th></span></q></dt></tr></i><div id='p6BEd'><tfoot id='p6BEd'></tfoot><dl id='p6BEd'><fieldset id='p6BEd'></fieldset></dl></div>
                    2. <legend id='p6BEd'><style id='p6BEd'><dir id='p6BEd'><q id='p6BEd'></q></dir></style></legend>

                    3. <tfoot id='p6BEd'></tfoot>