优秀的问题!我相信我有答案。这需要在C语言中深入研究Python源代码,所以请耐心等待。
首先,
format(obj, format_spec)
只是
obj.__format__(format_spec)
的语法糖。要具体查看此处发生的位置,您需要查看
abstract.c中的函数。
PyObject *
PyObject_Format(PyObject* obj, PyObject *format_spec)
{
PyObject *empty = NULL;
PyObject *result = NULL;
...
if (PyInstance_Check(obj)) {
HERE -> PyObject *bound_method = PyObject_GetAttrString(obj, "__format__");
if (bound_method != NULL) {
result = PyObject_CallFunctionObjArgs(bound_method,
format_spec,
NULL);
...
}
为了找到确切的调用,我们需要查看intobject.c文件:
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 中作为函数存在:
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;
if (format_spec_len == 0) {
result = STRINGLIB_TOSTR(obj); <- EXPLICIT CAST ALERT!
goto done;
}
...
}
因此,你的答案就在这里。简单检查format_spec_len
是否为0
,如果是,则将obj
转换为字符串。正如你所知道的那样,str(True)
是'True'
,谜底揭开了!
format(str(True),"^")
。 - jspurim