python 3.3 repr

Roy Smith roy at panix.com
Fri Nov 15 15:25:48 CET 2013


In article <b6db8982-feac-4036-8ec4-2dc720d41a4b at googlegroups.com>,
Ned Batchelder <ned at nedbatchelder.com> wrote:

> In Python3, repr() will return a Unicode string, and will preserve existing 
> Unicode characters in its arguments.  This has been controversial.  To get 
> the Python 2 behavior of a pure-ascii representation, there is the new 
> builtin ascii(), and a corresponding %a format string.

I'm still stuck on Python 2, and while I can understand the controversy ("It breaks my Python 2 code!"), this seems like the right thing to have done.  In Python 2, unicode is an add-on.  One of the big design drivers in Python 3 was to make unicode the standard.

The idea behind repr() is to provide a "just plain text" representation of an object.  In P2, "just plain text" means ascii, so escaping non-ascii characters makes sense.  In P3, "just plain text" means unicode, so escaping non-ascii characters no longer makes sense.

Some of us have been doing this long enough to remember when "just plain text" meant only a single case of the alphabet (and a subset of ascii punctuation).  On an ASR-33, your C program would print like:

MAIN() \(
	PRINTF("HELLO, ASCII WORLD");
\)

because ASR-33's didn't have curly braces (or lower case).

Having P3's repr() escape non-ascii characters today makes about as much sense as expecting P2's repr() to escape curly braces (and vertical bars, and a few others) because not every terminal can print those.

--
Roy Smith
roy at panix.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131115/f74305d9/attachment.html>


More information about the Python-list mailing list