Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'encoding': 0.05; 'ascii': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'garrett': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'unicode,': 0.16; '(not': 0.18; 'addition,': 0.20; 'code,': 0.22; 'otherwise,': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'code': 0.31; 'safely': 0.31; 'with,': 0.31; 'writes:': 0.31; 'could': 0.34; 'something': 0.35; 'there': 0.35; 'consistent': 0.36; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'simply': 0.61; 'times': 0.62; 'received:217': 0.63; 'myself': 0.63 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: dieter Subject: Re: encode and decode builtins Date: Mon, 17 Nov 2014 08:19:11 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e0aaf6.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:cR3auX8In2F1ipwtBsb3EQF3uCQ= 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: 16 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416208766 news.xs4all.nl 2873 [2001:888:2000:d::a6]:47268 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80962 Garrett Berg writes: > ... > However, there are times that I do not care what data I am working with, > and I find myself writing something like: > > if isinstance(data, bytes): data = data.decode() Apparently, below this code, you do care that "data" contains "str" (not "bytes") -- otherwise, you could simply drop this line. Note, in addition, that the code above might only work if "data" actually contains ASCII only bytes (at least in Python 2.x, it would fails otherwise). To safely decode bytes into unicode, you need to precisely know the encoding -- something which is not consistent with "I do not care about what data I am working with".