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


Groups > comp.lang.python > #82554

Re: Portable code: __import__ demands different string types between 2 and 3

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jeanpierreda@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'subject:code': 0.07; "'...'": 0.09; 'alias': 0.09; 'bytes,': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; '__future__': 0.16; 'above)': 0.16; 'finney': 0.16; 'literals': 0.16; 'subject: \n ': 0.16; 'subject:between': 0.16; 'unicode,': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'unicode': 0.24; 'cc:2**0': 0.24; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '3.2': 0.31; 'run': 0.32; 'text': 0.33; 'received:google.com': 0.35; '14,': 0.36; '2.6': 0.36; 'doing': 0.36; 'example,': 0.37; 'ben': 0.38; 'e.g.': 0.38; 'pm,': 0.38; 'ensure': 0.60; 'skip:u 10': 0.60; 'slowly': 0.60; "you'll": 0.62; 'making': 0.63; 'benefit': 0.68; 'believe,': 0.84; 'str.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=m4ZLWEGPca8ZzZZHBHlZzKQe3OL/cpwnOFEsb576SGs=; b=X7iD7nP5MS5Hdv2brYPHJnJiR75ZC7UGJNtOkPSMx6KytmPQV7vLurPVL6vU6y8Nhz mlX4VgX9NIyl7iWjFqS+LU/7x7mRyLdb0yQL6srzI9lK2T1nuOXrgCPfLb7nEjqymDhA yR0irCtsvEMKswLFCL9AyKuqK1W0XhZQPDvS7E9Z+T+Doiig4msmSeOn3wcoc1gdKLfg tbyhj3DtnJ0xmXgauOPJZwGUWwkswSEErySVaZbMe9Kqkcn5TF3PwvO5C5OgWlV+YvxA DchF6SycPyWVIILOaK3jE+jAD8T4N1i27qmD/+NUiDnZ+s7c6MMqJKHKMye9qVZ6XbAK ZY+g==
X-Received by 10.224.56.3 with SMTP id w3mr54474662qag.51.1418632739325; Mon, 15 Dec 2014 00:38:59 -0800 (PST)
MIME-Version 1.0
In-Reply-To <85wq5tpe0g.fsf@benfinney.id.au>
References <85wq5tpe0g.fsf@benfinney.id.au>
From Devin Jeanpierre <jeanpierreda@gmail.com>
Date Mon, 15 Dec 2014 00:38:18 -0800
Subject Re: Portable code: __import__ demands different string types between 2 and 3
To Ben Finney <ben+python@benfinney.id.au>
Content-Type text/plain; charset=UTF-8
Cc "comp.lang.python" <python-list@python.org>
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.16977.1418632748.18130.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418632748 news.xs4all.nl 2902 [2001:888:2000:d::a6]:37434
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82554

Show key headers only | View raw


On Sun, Dec 14, 2014 at 11:29 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> I'm slowly learning about making Python code that will run under both
> Python 2 (version 2.6 or above) and Python 3 (version 3.2 or above).
>
> This entails, I believe, the admonition to ensure text literals are
> Unicode by default::
>
>     from __future__ import unicode_literals

Ordinarily, for 2.x/3.3+ code I would suggest not doing this --
instead, b'...' for bytes, u'...' for unicode, and '...' for native
"string" type (bytes on 2.x, unicode on 3.x). This is the main benefit
of the u'...' syntax addition.

For 3.2, you'll have to do b'...' for bytes, '...' for unicode, and
str('...') for platform-specific strings (bytes on 2.x, unicode on
3.x).  It is in good taste to make an alias of str so it's less
confusing, e.g. native_str = str.

So for example, it's __import__(str("foo")), and getattr(foo, str("bar"), baz).

-- Devin

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


Thread

Re: Portable code: __import__ demands different string types between 2 and 3 Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-12-15 00:38 -0800

csiph-web