有人可以解释一下为什么在下面的示例中print a
会引发异常,而a.__str__()
不会?
>>> class A:
... def __init__(self):
... self.t1 = "čakovec".decode("utf-8")
... self.t2 = "tg"
... def __str__(self):
... return self.t1 + self.t2
...
>>> a = A()
>>> print a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u010d' in position 0: ordinal not in range(128)
>>> a.__str__()
u'\u010dakovectg'
>>> print a.__str__()
čakovectg