在Python3和Python2中,__repr__应该返回字节还是unicode?引用和引用将是理想的。
以下是一些信息关于2-3兼容性,但我看不出答案.
发布于 2018-01-16 01:36:57
类型为str ( python2.x和python3.x):
>>> type(repr(object()))
<class 'str'>必须这样做,因为如果__str__不存在,则默认调用__repr__,但是__str__必须返回一个str。
对于那些不知道的人,在python3.x中,str是代表unicode的类型。在python2.x中,str是表示字节的类型。
发布于 2018-01-16 01:38:16
这是两种语言中的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'>(里面有一个元组。)
https://stackoverflow.com/questions/48272906
复制相似问题