该类型为str
(适用于Python 2.x和Python 3.x):
>>> type(repr(object()))
<class 'str'>
__str__
,默认会调用__repr__
,但__str__
必须返回一个str
。对于不了解的人来说,在Python3.x中,str
是表示Unicode的类型。在Python2.x中,str
是表示字节的类型。str
是Unicode,而在Python2中,str
是字节。我认为OP正在寻找这些细节。 - cowbertstr
在两者中都是str
。Python3没有Unicode类型(就像Python2没有字节类型一样)。从语义上讲,如果你想表达的是这个意思,Python3的str
是Python2.x的unicode
的功能等效物... - mgilsonstr
是一个 Unicode 对象。 - cowbert__str __()
和__repr __()
的访问器要求返回类型设置为str
。在Py3k中,我尝试覆盖__str __()
和__repr __()
以返回b''
,并且所有的print()
(调用__str __()
),str()
和repr()
(调用__repr__()
)都会产生TypeError:... returned non-string (type bytes)
。 - cowbert在两种语言中都是str
:
Python 3.6.4 (default, Dec 21 2017, 18:54:30)
>>> type(repr(()))
<class 'str'>
Python 2.7.14 (default, Nov 7 2017, 17:59:11)
>>> type(repr(()))
<type 'str'>