Question: __repr__ can't print unicode right

Donn Cave donn at oz.net
Wed Nov 22 05:53:54 CET 2000


Quoth Walter Wong <h2owong at yahoo.com>:
| I am trying to write a class that handle Chinese big5 code.  But find
| some problem in printing the __repr__ value.
|
| >>> class chinese:
| 	def __init__(self, value):
| 		self.data =3D value
| 	def __repr__(self):
| 		return `self.data`
|
| Let's said I assign a chinese word to the class' instance.
| x.data and x should return the same unicode like this
|
| >>> x =3D chinese('=A7A')
| >>> x.data
| '\247A'
| >>> x
| '\247A'
|
| But when I try to print the two.  They come out differently.
|
| >>> print x.data
| =A7A
| >>> print x
| '\247A'
|
| Is there any way that I can print the chinese word right?
| >>> print x
| =A7A

If I understand right, you should add a __str__ method along
with __repr__.  This might work for you:

   def __str__(self):
       return self.data

I don't know if you will have any trouble with unicode or
big 5, but at least print won't display a quoted string
if you do this.

	Donn Cave, donn at oz.net





More information about the Python-list mailing list