[Python-Dev] PEP 292-related: why string substitution is not the same operation as data formatting

Lalo Martins lalo@laranja.org
Sun, 23 Jun 2002 15:16:30 -0300


For a moment, please remove for your mind your experience of C and printf.
Meditate with me and picture yourself in a happy world of object-orientation
and code readability, where everything cryptic and obscured is banished.

Just to help you do that, I'll avoid the notation chosen by the PEP. Let's
use, for the duration of this post, the hypothetic notation suggested by
some other reader: "<<name>> is from <<country>>".

Now, this thing we're talking about is replacing parts of the string with
other strings. These strings may be the result of running some non-string
objects trough str(foo) - but, we are making no assumptions about these
objects. Just that str(foo) is somehow meaningful. And, to my knowledge,
there are no python objects for which str(foo) doesn't work.

So, string substitution is non-intrusive.

Also, if you keep your templates (let's call a string containing
substitution markup a template, shall we?) outside your source code, as is
the case with i18n, pure substitution doesn't require the people who edit
them (for example, translators) to know anything about python *or* even
programming.

String substitution only depends on an identifier ('name' or 'country'), no
sick abbreviations like 's' or 'd' or 'f' or 'r' or 'x' that you have to
keep a table for.

So, string substitution is readable and non-cryptic.



Now, data formatting is another animal entirely. It's a way to request one
specific representation of a piece of data.

But there is a catch. When you do '%8.3d' % foo you are *expecting* that foo
a floating-point number and you know you'll get TypeError otherwise. This
is, IMO, invasive. In my ideal OO-paradise I would rather have something
like foo.format(8, 3) (THIS IS NOT A PEP!).

IMO, if you, as I asked in the first paragraph, pretend you don't know C and
printf and python's % operator and then pretend you're having your first
contact with it, while already having some experience with python's
readability, it's hard not to be shocked. And I bet you'd go to great
lengths to avoid using the "feature".



Conclusion: I think string formatting is a cryptic and obscure misfeature
inherited from C that should be deprecated in favour of something less
invasive and more readable/explicit.

More, I'm completely opposed to "<<name>> is <<age:.0d>> years old" because
it's still cryptic and invasive. This should instead read similar to
"<<name>> is <<age>> years old".sub({'name': x.name, 'age': x.age.format(None, 0)})



Guido, can you please, for our enlightenment, tell us what are the reasons
you feel %(foo)s was a mistake?

[]s,
                                               |alo
                                               +----
--
  It doesn't bother me that people say things like
   "you'll never get anywhere with this attitude".
   In a few decades, it will make a good paragraph
      in my biography. You know, for a laugh.
--
http://www.laranja.org/                mailto:lalo@laranja.org
         pgp key: http://www.laranja.org/pessoal/pgp

Python Foundry Guide http://www.sf.net/foundry/python-foundry/