1
有人能解釋我爲什麼在下面的例子中,print a
引發異常,而a.__str__()
不是?Python unicode錯誤
>>> 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