Should __repr__ return bytes or unicode?

In Python 3 and Python 2, __repr__should it return bytes or unicode? Link and quote would be perfect.

Here is some info on 2-3 compatibility, but I don't see the answer.

+4
source share
2 answers

Type str(for python2.x and python3.x):

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

This should be so, because __str__by default it calls __repr__if the first is not present, but __str__should return str.

For those who don’t know, in python3.x stris a type that is unicode. In python2.x, stris a type representing bytes.

+7
source

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'>

( .)

+1

Source: https://habr.com/ru/post/1692240/


All Articles