New-style string formatting

Gerrit Holl gerrit at nl.linux.org
Tue May 20 15:33:24 EDT 2003


Hi,

Skip Montanaro schreef op dinsdag 20 mei om 18:22:45 +0000:
>     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.

True.

> 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)?

I don't think this is a really valid argument, because exactly the same is
true for string interpolation using the % character.

>     * What's intuitive about calling a string?

Exactly as much as there is intuitive on modulo'ing a string.

>       There's nothing mnemonic in
>       the construct to suggest what you want to do.

True. Mnemonic would be to create a ".format" method.

>       The % operator at least
>       has the syntactic connection to C's printf() function.

The syntactic connection goes as far as the % as a formatting character.
I don't know C very well, but AFAIK, % is not an operator for strings
(character sequences) in C. CMIIW, but printf's first argument corresponds
to Python's % left hand operand, and printf's second argument corresponds
to Python's % right hand operand.

So, where _does_ the % as string operator come from?

>     * This idea doesn't extend to dictionary interpolation

It does: see my other message: "..."(**d) and "..."(*s).

>     * There's already a perfectly good way to do it: "..." % (args).

True. This works very well.

>       Adding
>       another way that only saves a single character and goes against the
>       Pythonic grain of having generally one way to do things.

True. I am not sure about it yet, because different Zen of Python advices
contradict each other here.
+ "Beatiful is better than ugly": I find the % not very beatiful.
- "Explicit is better than implicit": It's more explicit with the % sign.
+ "Simple is better than complex": % does complicate a little bit. 
- "Sparse is better than dense": it removes a character ;).
-"There should be one-- and preferably only one --obvious way to do it."
  ...but what is obvious? It's only what we're used too - does that mean
  it's the best?
+ "If the implementation is easy to explain, it may be a good idea."

>       (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.)

It surely would be a Py3K issue to deprecate it. That is the biggest problem
of this proposal: it's like internet beats. It may be a great idea, but I
should have been there 13 years ago - they'd see me come, a 4-year-old at
the CWI :-)

yours,
Gerrit.

-- 
205. If the slave of a freed man strike the body of a freed man, his
ear shall be cut off. 
        -- Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list