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

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

        <legend id='pXnoR'><style id='pXnoR'><dir id='pXnoR'><q id='pXnoR'></q></dir></style></legend><tfoot id='pXnoR'></tfoot>

          <bdo id='pXnoR'></bdo><ul id='pXnoR'></ul>
      1. 在 Python 中使用 format() 方法打印布尔值 True/False

        时间:2023-09-01

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

            <tbody id='yxqy8'></tbody>

                <bdo id='yxqy8'></bdo><ul id='yxqy8'></ul>
                • <i id='yxqy8'><tr id='yxqy8'><dt id='yxqy8'><q id='yxqy8'><span id='yxqy8'><b id='yxqy8'><form id='yxqy8'><ins id='yxqy8'></ins><ul id='yxqy8'></ul><sub id='yxqy8'></sub></form><legend id='yxqy8'></legend><bdo id='yxqy8'><pre id='yxqy8'><center id='yxqy8'></center></pre></bdo></b><th id='yxqy8'></th></span></q></dt></tr></i><div id='yxqy8'><tfoot id='yxqy8'></tfoot><dl id='yxqy8'><fieldset id='yxqy8'></fieldset></dl></div>
                  <legend id='yxqy8'><style id='yxqy8'><dir id='yxqy8'><q id='yxqy8'></q></dir></style></legend>
                • <tfoot id='yxqy8'></tfoot>
                • 本文介绍了在 Python 中使用 format() 方法打印布尔值 True/False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图打印布尔表达式的真值表.在执行此操作时,我偶然发现了以下内容:

                  I was trying to print a truth table for Boolean expressions. While doing this, I stumbled upon the following:

                  >>> format(True, "") # shows True in a string representation, same as str(True)
                  'True'
                  >>> format(True, "^") # centers True in the middle of the output string
                  '1'
                  

                  只要我指定了格式说明符,format() 就会将 True 转换为 1.我知道 boolint 的子类,因此 True 的计算结果为 1:

                  As soon as I specify a format specifier, format() converts True to 1. I know that bool is a subclass of int, so that True evaluates to 1:

                  >>> format(True, "d") # shows True in a decimal format
                  '1'
                  

                  但是为什么在第一个示例中使用格式说明符会将 'True' 更改为 1?

                  But why does using the format specifier change 'True' to 1 in the first example?

                  我求助于文档进行说明.它唯一说的是:

                  I turned to the docs for clarification. The only thing it says is:

                  一般约定是空格式字符串 ("") 产生的结果与您对值调用 str() 产生的结果相同.非空格式字符串通常会修改结果.

                  A general convention is that an empty format string ("") produces the same result as if you had called str() on the value. A non-empty format string typically modifies the result.

                  因此,当您使用格式说明符时,字符串会被修改.但是,如果指定了对齐运算符(例如^),为什么从True更改为1?p>

                  So the string gets modified when you use a format specifier. But why the change from True to 1 if only an alignment operator (e.g. ^) is specified?

                  推荐答案

                  好问题!我相信我有答案.这需要在 C 语言中挖掘 Python 源代码,所以请耐心等待.

                  Excellent question! I believe I have the answer. This requires digging around through the Python source code in C, so bear with me.

                  首先,format(obj, format_spec) 只是 obj.__format__(format_spec) 的语法糖.对于发生这种情况的具体位置,您必须查看 abstract.c,在函数中:

                  First, format(obj, format_spec) is just syntactic sugar for obj.__format__(format_spec). For specifically where this occurs, you'd have to look in abstract.c, in the function:

                  PyObject *
                  PyObject_Format(PyObject* obj, PyObject *format_spec)
                  {
                      PyObject *empty = NULL;
                      PyObject *result = NULL;
                  
                      ...
                  
                      if (PyInstance_Check(obj)) {
                          /* We're an instance of a classic class */
                  HERE -> PyObject *bound_method = PyObject_GetAttrString(obj, "__format__");
                          if (bound_method != NULL) {
                              result = PyObject_CallFunctionObjArgs(bound_method,
                                                                    format_spec,
                                                                    NULL);
                  
                      ...
                  }
                  

                  要找到确切的调用,我们必须查看 intobject.c:

                  To find the exact call, we have to look in intobject.c:

                  static PyObject *
                  int__format__(PyObject *self, PyObject *args)
                  {
                      PyObject *format_spec;
                  
                      ...
                  
                      return _PyInt_FormatAdvanced(self,
                                       ^           PyBytes_AS_STRING(format_spec),
                                       |           PyBytes_GET_SIZE(format_spec));
                                 LET'S FIND THIS
                      ...
                  }
                  

                  _PyInt_FormatAdvanced 实际上在 formatter_string.c 作为 formatter.h:

                  _PyInt_FormatAdvanced is actually defined as a macro in formatter_string.c as a function found in formatter.h:

                  static PyObject*
                  format_int_or_long(PyObject* obj,
                                 STRINGLIB_CHAR *format_spec,
                             Py_ssize_t format_spec_len,
                             IntOrLongToString tostring)
                  {
                      PyObject *result = NULL;
                      PyObject *tmp = NULL;
                      InternalFormatSpec format;
                  
                      /* check for the special case of zero length format spec, make
                         it equivalent to str(obj) */
                      if (format_spec_len == 0) {
                          result = STRINGLIB_TOSTR(obj);   <- EXPLICIT CAST ALERT!
                          goto done;
                      }
                  
                      ... // Otherwise, format the object as if it were an integer
                  }
                  

                  这就是你的答案.简单检查 format_spec_len 是否为 0,如果是,将 obj 转换为字符串.众所周知,str(True)就是'True',谜底结束!

                  And therein lies your answer. A simple check for whether format_spec_len is 0, and if it is, convert obj into a string. As you well know, str(True) is 'True', and the mystery is over!

                  这篇关于在 Python 中使用 format() 方法打印布尔值 True/False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将数据框中的真假值转换为 1 为真,0 为假 下一篇:检查 False 的正确方法是什么?

                  相关文章

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

                  <tfoot id='b9WXH'></tfoot>

                      <bdo id='b9WXH'></bdo><ul id='b9WXH'></ul>

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