str()正如我在以下代码中发现的那样,精美的手册没有说明提供三个参数时该方法的作用requests/models.py:
content = str(self.content, encoding, errors='replace')
这是在哪里记录的?它有什么作用?
那不是内置str函数。看着那(这顶部的导入:
from .compat import (
cookielib, urlparse, urlunparse, urlsplit, urlencode, str, bytes, StringIO,
is_py2, chardet, json, builtin_str, basestring)
Kenneth 已经定义了他自己的compat模块以兼容 Python 2 和 3,并且他覆盖了几个内置函数,包括str.
正如您在该模块中看到的那样,在 Python 2 中它别名unicode为str,因此它的工作方式与 Python3 几乎相同str。
您正在阅读版本 2 的文档,但正在查看使用(或匹配)Python 3 的代码。
第 3 版的文档说:
str(object='') str(object=b'', encoding='utf-8', errors='strict')返回对象的 str 版本。有关详细信息,请参见 str()。
在链接之后,关于encodinganderrors关键字参数的内容如下:
如果给出了
encodingor中的至少一个,则应该是-like 对象(例如or )。在这种情况下,如果是(或)对象,则等价于。否则,在调用之前获取对象底层的对象。errorsobjectbytesbytesbytearrayobjectbytesbytearraystr(bytes, encoding, errors)bytes.decode(encoding, errors)bytesbufferbytes.decode()
To add to daniel-roseman's answer here's the documentation and what does it do.. Since str in the stated code represents unicode.