[Python-3000] format() method and % operator

Jim Jewett jimjjewett at gmail.com
Fri Aug 17 18:47:12 CEST 2007


On 8/17/07, Victor Stinner <victor.stinner at haypocalc.com> wrote:
> But for me it looks to be more complex: we have to maintain indexes (0, 1,
> 2, ...), marker is different ({0} != {1}), etc.

> ... tell me if it would be possible to write simply:
>    "{} {}".format('Hello', 'World')

It would be possible to support that, but I think it was excluded
intentionally, as a nudge toward more robust formatting strings.

(1)  Translators may need to reorder the arguments.  So the format
string might change from
    "{0} xxx {1}"
to a more idiomatic (in the other language)
    "yyy {1} {0}"

This doesn't by itself rule out {} for the default case, but being
explicit makes things more parallel, and easier to verify.

(2)  You already have to maintain indices mentally; it is just
bug-prone on strings long enough for the formatting language to
matter.  For example, if

    gossip="%s told %s that %"
changes to
    gossip="%s told %s on %s that %"

Then in some other part of the program, you will also have to change
    gossip % (name1, name2, msg)
to
    gossip % (name1, date, name2, msg)

Using a name mapping (speaker=, ... hearer=..., ) is a better answer,
but explicit numbers are a halfway measure.

-jJ


More information about the Python-3000 mailing list