The fine manual没有说明str()
方法在提供三个参数时所执行的操作,正如我在requests/models.py
的代码中找到的那样:
content = str(self.content, encoding, errors='replace')
这在哪里记录?它做了什么?
答案 0 :(得分:7)
这不是内置的str
功能。看看imports at the top:
from .compat import (
cookielib, urlparse, urlunparse, urlsplit, urlencode, str, bytes, StringIO,
is_py2, chardet, json, builtin_str, basestring)
Kenneth为Python 2和3之间的兼容性定义了自己的compat
模块,并且他覆盖了几个内置函数,包括str
。
作为you can see in that module,在Python 2中,它将unicode
别名为str
,因此它与Python3 str
几乎相同。
答案 1 :(得分:3)
您正在阅读第2版的文档,但使用(或匹配)Python 3查看代码。
str(object='') str(object=b'', encoding='utf-8', errors='strict')
返回str版本的对象。有关详细信息,请参阅str()。
在链接后面会说明encoding
和errors
个关键字参数:
如果至少提供了
encoding
或errors
中的一个,object
应该是类似bytes
的对象(例如bytes
或bytearray
})。在这种情况下,如果object
是bytes
(或bytearray
)对象,则str(bytes, encoding, errors)
相当于bytes.decode(encoding, errors)
。否则,在调用bytes
之前获取buffer
对象底层的bytes.decode()
对象。
答案 2 :(得分:1)
在这里添加daniel-roseman的答案是documentation and what does it do.。由于所述代码中的str
代表unicode
。