Question: __repr__ can't print unicode right

Carsten Geckeler geckeler at gmx.net
Wed Nov 22 07:00:55 EST 2000


On Wed, 22 Nov 2000, Walter Wong wrote:

> 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 = 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 = chinese('§A')
> >>> x.data
> '\247A'
> >>> x
> '\247A'
> 
> But when I try to print the two.  They come out differently.
> 
> >>> print x.data
> §A
> >>> print x
> '\247A'
> 
> Is there any way that I can print the chinese word right?
> >>> print x
> §A

I think the problem is the return value of your __repr__ method.  Let's
try:

	def __repr__(self):
		return self.data

The difference is that this one has NO back quotes `.  Then it works

Cheers, Carsten
-- 
Carsten Geckeler:  geckeler at gmx dot net





More information about the Python-list mailing list