[Python-Dev] Re: PEP 292, Simpler String Substitutions

Neal Norwitz neal@metaslash.com
Sun, 23 Jun 2002 23:16:20 -0400


I'm pretty negative on string interpolation, I don't see it
as that useful or %()s as that bad.  But obviously, many others
do feel there is a problem.

I don't like the schism that $ vs. % would create.  Nor do
I like many other proposals.  So here is yet another proposal:

 * Add new builtin function interp() or some other name:
     def interp(format, uselocals=True, useglobals=True, dict={}, **kw)
 * use % as the format character and allow optional () or {}
   around the name
 * if this is acceptable, {name:format_modifiers} 
   could be added in the future

Code would then look like this:

	>>> x = 5
	>>> print interp('x = %x')
	x = 5
	>>> print interp('x = %(x)')
	x = 5
	>>> print interp('x = %{x}')
	x = 5
	>>> print interp('y = %y')
	NameError: name 'y' is not defined
	>>> print interp('y = %y', dict={'y': 10})
	y = 10
	>>> print interp('y = %y', y=10)
	y = 10

This form:
 * eliminates any hint of $
 * is similar to current % handling, 
   but hopefully fixes the current deficiencies
 * allows locals and/or globals to be used
 * allows any dictionary/mapping to be used
 * allows keywords
 * is extensible to allow for formatting in the future
 * doesn't require much extra typing or thought

Now I'm sure everyone will tell me how awful this is. :-)

Neal

PS  I'm -0 on this proposal.  And I dislike the name interp.