__repr__ 应该是 return 字节还是 unicode?

Is __repr__ supposed to return bytes or unicode?

在 Python 3 和 Python 2 中,__repr__ 应该是 return 字节还是 unicode?参考和报价将是理想的。

Here's some information 关于 2-3 兼容性,但我没有看到答案。

类型是str(对于python2.x和python3.x):

>>> type(repr(object()))
<class 'str'>

必须如此,因为如果前者不存在,__str__ 默认调用 __repr__,但是 __str__ 必须 return一个str.

对于那些不知道的人,在python3.x中,str是代表unicode的类型。在python2.x中,str是表示字节的类型。

两种语言都是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'>

(里面有一个元组。)