New-style string formatting

Gerrit Holl gerrit at nl.linux.org
Wed May 21 04:49:26 EDT 2003


Skip Montanaro schreef op dinsdag 20 mei om 22:16:28 +0000:
>     >> * Who died and made string interpolation king?  In other words, why
>     >> should "..."(args) be interpreted as string interpolation and not
>     >> "...".replace(args)?
> 
>     Gerrit> I don't think this is a really valid argument, because exactly
>     Gerrit> the same is true for string interpolation using the % character.
> 
> Yes, except for history.  That is, I have the benefit of arguing from the
> position that "..."%(args) is already in place.

True.

>     >> There's nothing mnemonic in the construct to suggest what you want to
>     >> do.
> 
>     Gerrit> True. Mnemonic would be to create a ".format" method.
> 
> According to the definition I found for "mnemonic" via Google, as an
> adjective it means:
> 
>     Relating to, assisting, or intended to assist the memory.

It is called an 'ezelsbruggetje', according to my dictionairy.

> In that sense, "%" is mnemonic for me because it reminds me of the %-strings
> in C printf() format strings.  "format" is descriptive.  It would probably
> be as good a choice as either syntactic construct, largely because it's more
> explicit and requires less knowledge of convention or printf().

True. That is the same 'history' argument as used earlier, but that doesn't
mean it's a strong (probably decisive) argument.

>     Gerrit> The syntactic connection goes as far as the % as a formatting
>     Gerrit> character.  I don't know C very well, but AFAIK, % is not an
>     Gerrit> operator for strings (character sequences) in C.
> 
> Nope, but ignoring the %(key)s form added to Python, the meaning of Python's
> format strings and printf()'s are almost identical.

True.

>     Gerrit> CMIIW,
>             ???

"Correct me if I'm wrong" (shouldn't have abbreviated that, sorry).

>     Gerrit> So, where _does_ the % as string operator come from?
> 
> Only that it is mnemonic - suggestive of printf()'s format character.  Like
> I said above, I have history on my side. "..."%(args) already exists in the
> language and is not likely to disappear.  If it didn't, your proposal would
> be much stronger.

Yes, it's true.

My question was actually historic; what is the history of printf?
Does it come from C or is it older?

>     Gerrit> + "Simple is better than complex": % does complicate a little
>     Gerrit>   bit.  
> 
> Microscopically?

Yes, microscopically.

>     Gerrit> + "If the implementation is easy to explain, it may be a good
>     Gerrit>   idea." 
> 
> I don't think one is really easier to explain than the other.

True.

> In general, when proposing to replace one construct of the language with
> another, Guido often raises some barriers:
> 
>     * code breakage - "..."(args) clearly wouldn't break anything, since
>       it's currently syntactically invalid.

Formally, it's not really a new syntax but a new method: (...) as shortcut
to .__call__(...) already exists. It only adds a new method.

>     * invalidation of a substantial body of knowledge - in this case, no
>       Python book mentions any syntactic construct like "..."(args). [1]
>       "..."(args) would clearly get used, so people would look to their
>       current books and find them lacking.

True.

>     * does it significantly increase of expressive power or clarity of the
>       language?  No matter that you like "..."(args) better than
>       "..."%(args), the former is is clearly not head and shoulders better
>       than the latter in this respect. [2]

True.

> In some sense you have two strikes against you.  I don't see "..."(args)
> clearing either the second or third barriers.

True.

> [2] Aside: Would "..."(args) be more efficient than "..."%(args)?

No, not at all...

I think I am convinced. Actually, the only argument against is historical,
but it is a convincing argument. Just as internet beats won't replace
hours and minutes, volapuk or esperanto won't replace English and dvorak
keyboards will never replace qwerty; without an extremely strong and
autistic dictator, combined with memory holes, this change is unrealistic ;)

yours,
Gerrit.

-- 
243. As rent of herd cattle he shall pay three gur of corn to the
owner. 
        -- 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