请考虑下列打字本:
>>> s = 'a'
>>> isinstance(s, bytes)
True
>>> isinstance(s, str)
True
>>> isinstance(s, unicode)
False
>>> isinstance(s.decode('utf-8'), unicode)
True为什么s既是str又是bytes?其中一个是另一个的后代吗?
我是怎么碰到的?我试图在文档中找到decode方法的描述。我无法为str找到它,但能够为bytes找到它。
发布于 2016-11-17 15:20:03
你看错文件了。
这种等价只适用于Python2.7。在那里,为了方便迁移到Python3,引入了bytes作为str的别名。
在Python3中,str是以前称为unicode的类型,bytes是以前称为str的类型。
Python2的str.decode文档是这里。
https://stackoverflow.com/questions/40658535
复制相似问题