On Sun, Sep 17, 2017 at 3:27 PM, Peter Otten <__pete...@web.de> wrote:

> leam hall wrote:
>
> > Doesn't seem to work. The failing code takes the strings as is from the
> > database. it will occasionally fail when a name comes up that uses
> > a non-ascii character.
>
> Your problem in nuce: the Python 2 __str__() method must not return
> unicode.
>
> >>> class Character:
> ...     def __str__(self): return u"Brösel"
> ...
> >>> print(Character())
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in
> position
> 2: ordinal not in range(128)
>
> While you may define a __unicode__ method it has to be called explicitly:
>
> >>> class Character:
> ...     def __unicode__(self): return u"Brösel"
> ...
> >>> print(Character())
> <__main__.Character instance at 0x7fc10020f5a8>
> >>> print(unicode(Character()))
> Brösel
>
> Another alternative is to convert explicitly, to some encoding, and hope it
> works in the actual environment:
>
> >>> class Character:
> ...     def __unicode__(self): return u"Brösel"
> ...     def __str__(self): return unicode(self).encode("utf-8")
> ...
> >>> print(Character())
> Brösel
>

Ah! So this works in Py2:
   def __str__(self):
     name    = self.name.encode("utf-8")


It completely fails in Py3:
  PVT b'Lakeisha F\xc3\xa1bi\xc3\xa1n' 7966A4     [F] Age: 22


Note that moving __str__() to display() gets the same results. Not sure it
is an issue with __str__.



> The more you think about it the more attractive a switch to Python 3 will
> appear.
>

Not for me, actually. I'm trying to learn better OOP and coding in general.
I'm using Python because there's a work related use case for Py2. There
isn't one for Py3. If the work use case it removed then there are lots of
languages to try out.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to