[Python-Dev] string formatting options and removing basestring.__mod__ (WAS: Replacement for print in Python 3.0)
Gareth McCaughan
gmccaughan at synaptics-uk.com
Mon Sep 5 17:48:13 CEST 2005
> If people know of other languages that have a different approach to
> string formatting, it might be useful to see them.
Common Lisp has something broadly C-like but bigger and hairier.
It includes powerful-but-confusing looping and conditional
features, letting you say things like
(format t "~{~^, ~A~}" 1 2 3 "wombat")
which produces
1, 2, 3, wombat
or -- you may wish to be sitting down before reading further --
(format t "~#[nothing~;~S~;~S and ~S~:;~@{~#[~; and~] ~S~^ ,~}~]." 1 2 3 "wombat")
which produces
1, 2, 3, and wombat
and also does the Right Thing with 0, 1 or 2 items. (The first
argument to FORMAT, in case you were wondering, determines where
the output should go. Feeding in T, as here, sends it to stdout.
You can also give it an arbitrary stream, or NIL to return the
formatted result as a string.)
For the impressive and horrifying full story, see
http://www.lisp.org/HyperSpec/Body/sec_22-3.html
Most of the features of CL's formatted output are probably,
shall we say, inappropriate for Python. It might still be worth
a look, to see if there's anything under the rococo exterior
that would fit.
*
Some languages have "picture" formats, where the structure
of the format string more closely mimics that of the desired
output. (This is true, e.g., of some Basics and of one variety
of Perl output.) The trouble with this is that it limits how
much information you can provide about *how* each value is
to be formatted within the available space.
*
C++'s << operator represents another way to do formatted
output. I regard it as an object lesson in bad design.
--
g
More information about the Python-Dev
mailing list