Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none:': 0.05; 'none):': 0.07; 'python': 0.09; 'bytes)': 0.09; 'encode': 0.09; 'non- string': 0.09; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'self.data': 0.09; 'subject:characters': 0.09; 'typeerror:': 0.09; 'url:unicode': 0.09; 'def': 0.10; ':-)': 0.13; 'encoding': 0.15; 'emanuele': 0.16; 'pytest': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'stuff.': 0.16; 'string': 0.17; 'comparing': 0.17; 'string,': 0.17; 'trying': 0.21; 'import': 0.21; '"",': 0.22; '2.x': 0.22; "haven't": 0.23; 'header:User-Agent:1': 0.26; '(most': 0.27; 'plain': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; '>>>>': 0.29; 'writes:': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'returned': 0.30; '(and': 0.32; 'file': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'there': 0.35; 'received:org': 0.36; 'subject:': 0.36; 'but': 0.36; 'method': 0.36; 'two': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'notice': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'received:79': 0.61; 'kind': 0.61; 'between': 0.63; '8bit%:43': 0.65; 'surprise': 0.65; 'skip:\xe2 10': 0.66; 'quando': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Lele Gaifax Subject: Re: Printing characters outside of the ASCII range Date: Sun, 11 Nov 2012 18:09:31 +0100 Organization: Nautilus Entertainments References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> <90c86fc7-a462-4a19-b883-17c64244c806@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: host183-110-dynamic.10-79-r.retail.telecomitalia.it User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2.50 (gnu/linux) Cancel-Lock: sha1:5gj0vijQg4d+qgDQDTInxV7Kbek= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352653786 news.xs4all.nl 6949 [2001:888:2000:d::a6]:55124 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33131 danielk 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 "", line 1, in > 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@metapensiero.it | -- Fortunato Depero, 1929.