Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80860

Re: encode and decode builtins

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.070
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'ambiguity': 0.09; 'objects,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'api': 0.11; 'python': 0.11; "wouldn't": 0.14; 'caring': 0.16; 'finney': 0.16; 'garrett': 0.16; 'pythonic': 0.16; 'rather,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'input': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'switch': 0.26; 'header:X-Complaints-To:1': 0.27; 'to?': 0.30; 'code': 0.31; 'them?': 0.31; 'with,': 0.31; 'writes:': 0.31; 'know.': 0.32; 'thanks!': 0.32; 'text': 0.33; 'something': 0.35; 'there': 0.35; 'data,': 0.36; 'method': 0.36; 'possible': 0.36; 'two': 0.37; 'clear': 0.37; 'ben': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'explain': 0.39; 'embedded': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'is.': 0.60; 'ago,': 0.61; 'times': 0.62; 'hear': 0.63; 'myself': 0.63; 'face': 0.64; 'provide': 0.64; 'more': 0.64; 'love': 0.65; 'great': 0.65; 'god': 0.65; 'skip:\xe2 10': 0.65; '8bit%:21': 0.69; '8bit%:46': 0.78; 'devices,': 0.84; 'or...': 0.84; 'received:125': 0.84; 'refuses': 0.84; '1988': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: encode and decode builtins
Date Sun, 16 Nov 2014 18:50:45 +1100
References <CALW7ahxWU_N87oAz0JJoXEiyLO0Erx-YggVmQgggzxST6Ra6XQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:pFiyHpbnO9kcUSso/Z6YyUKujdU=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15873.1416124257.18130.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416124257 news.xs4all.nl 2854 [2001:888:2000:d::a6]:50411
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80860

Show key headers only | View raw


Garrett Berg <googberg@gmail.com> writes:

> I made the switch to python 3 about two months ago, and I have to say
> I love everything about it, *especially* the change to using only
> bytes and str (no more unicode! or... everything is unicode!) As
> someone who works with embedded devices, it is great to know what data
> I am working with.

THanks! It is great to hear from people directly benefiting from this
clear distinction.

> 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()

Why are you in a position where ‘data’ is not known to be bytes? If you
want ‘unicode’ objects, isn't the API guaranteeing to provide them?

> This is tedious and breaks the pythonic method of not caring about
> what your input is.

I wouldn't call that Pythonic. Rather, in the face of ambiguity (“is
this text or bytes?”), Pythonic code refuses the temptation to guess:
you need to clarify what you have as early as possible in the process.

> If I expect that my input can always be decoded into valid data, then
> why do I have to write this?

I don't know. Why do you have to?

-- 
 \     “God was invented to explain mystery. God is always invented to |
  `\     explain those things that you do not understand.” —Richard P. |
_o__)                                                    Feynman, 1988 |
Ben Finney

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: encode and decode builtins Ben Finney <ben+python@benfinney.id.au> - 2014-11-16 18:50 +1100

csiph-web