New-style string formatting (Was: Re: Tuple "detection")

Skip Montanaro skip at pobox.com
Tue May 20 12:21:24 EDT 2003


    Gerrit> What Alexander proposes and I support is not to do string
    Gerrit> interpolation, but to simply change the syntax because the '%'
    Gerrit> is redundant; it's a much smaller change:

    Gerrit> "bla %s blo %d" % ("blu", 42) # becomes
    Gerrit> "bla %s blo %d" ("blu", 42)

Just because it's not a big change doesn't mean it's worthwhile.  Here are
a couple counterarguments:

    * Who died and made string interpolation king?  In other words, why
      should "..."(args) be interpreted as string interpolation and not
      "...".replace(args)?

    * What's intuitive about calling a string?  There's nothing mnemonic in
      the construct to suggest what you want to do.  The % operator at least
      has the syntactic connection to C's printf() function.

    * This idea doesn't extend to dictionary interpolation (without a change
      to the language syntax), which for interpolations with many arguments
      is less error-prone because you specifically indicate what value to
      insert where instead of relying on positional correctness.

    * There's already a perfectly good way to do it: "..." % (args).  Adding
      another way that only saves a single character and goes against the
      Pythonic grain of having generally one way to do things.  (You clearly
      couldn't deprecate the current syntax at this point.  It's too widely
      used.  It's not considered a mistake as Guido eventually concluded
      about integer division, for example.)

Skip





More information about the Python-list mailing list