Printing characters outside of the ASCII range
Lele Gaifax
lele at metapensiero.it
Sun Nov 11 12:09:31 EST 2012
danielk <danielkleinad at gmail.com> writes:
> Ian's solution gives me what I need (thanks Ian!). But I notice a
> difference between '__str__' and '__repr__'.
>
> class Pytest(str):
> def __init__(self, data = None):
> if data == None: data = ""
> self.data = data
>
> def __repr__(self):
> return (self.data).encode('cp437')
>
The correct way of comparing with None (and in general with
“singletons”) is with the “is” operator, not with “==”.
> If I change '__repr__' to '__str__' then I get:
>
>>>> import pytest
>>>> p = pytest.Pytest("abc" + chr(178) + "def")
>>>> print(p)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: __str__ returned non-string (type bytes)
In Python 3.3 there is one kind of string, the one that under Python 2.x
was called “unicode”. When you encode such a string with a specific
encoding you obtain a plain “bytes array”. No surprise that the
__str__() method complains, it's called like that for a reason :)
> I'm trying to get my head around all this codecs/unicode stuff. I
> haven't had to deal with it until now but I'm determined to not let it
> get the best of me :-)
Two good readings on the subject:
- http://nedbatchelder.com/text/unipain.html
- http://www.joelonsoftware.com/articles/Unicode.html
ciao, lele.
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it | -- Fortunato Depero, 1929.
More information about the Python-list
mailing list