In Python 2 str
must return an ASCII string. When you call __str__
directly you're skipping the step of Python converting the output of __str__
to an ASCII string (you could in fact return whatever you want from __str__
, but you shouldn't). __str__
should not return a unicode
object, it should return a str
object.
Here's something you can do instead:
In [29]: class A(object):
...: def __init__(self):
...: self.t1 = u"c∃".encode('utf8')
...: def __str__(self):
...: return self.t1
...:
In [30]: a = A()
In [31]: print a
c∃
In [32]: str(a)
Out[32]: 'c\xe2\x88\x83'
In [33]: a.__str__()
Out[33]: 'c\xe2\x88\x83'