Problem with __str__ method and character encoding

gialloporpora gialloporpora at gmail.com
Fri Dec 7 10:05:44 EST 2012


Risposta al messaggio di Chris Angelico :

> Your __str__ method is not returning a string. It's returning a
> Unicode object. Under Python 2 (which you're obviously using, since
> you use print as a statement), strings are bytes. The best thing to do
> would be to move to Python 3.3, in which the default string type is
> Unicode, and there's a separate 'bytes' type for communicating with
> file systems and networks and such. But if that's not possible, I
> would recommend having a separate method for returning a Unicode
> string (the same as your current __str__ but with a different name),
> and have __str__ call that and encode it - something like this:
>
> def describe(self):
>      return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr)
> def __str__(self):
>      return self.describe().encode(self._encoding)
>
> But it'd definitely be better to move to Python 3.
>

Thanks, now I have understood.
I have problem with the installer of the Python 3.3 (and 2.7.3), when I 
fix these problem I'll try to update to Python 3.3.

Sandro


-- 
*Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc
Sto ascoltando: *Robert Plant – One More Cup of Coffee (Valley Below) * 
- http://bit.ly/TtyHMq



More information about the Python-list mailing list