[Python-Dev] PEP 216 (string interpolation) alternative EvalDict
Skip Montanaro
skip@pobox.com
Mon, 14 Jan 2002 21:56:47 -0600
Paul> But then you want to interpolate the result of a function call or
Paul> expression. So you have to set up a one-time-use variable.
As has been demonstrated, there are several ways to tackle this problem. I
first saw something headed in this direction with Zope's (actually
DocumentTemplate's) MultiMapping class several years ago. It only aimed to
make it easy to interpolate named parameters from several dictionaries
simultaneously. Steve Majewski and others have shown how you can do this
with an EvalDict type of class, so it's not like you can't do this today.
The point is for something to be really worth modifying the syntax of the
language I think it has to demonstrate that it's significantly better than
the alternatives. The security argument is a red herring. There are enough
other ways programmers can blow their feet off. If someone is naive enough
to execute the moral equivalent of
print raw_input() % EvalDict3()
in their programs they will probably learn fairly quickly that it's a
questionable programming practice.
Paul> PEP 215 (which I did not write!) unifies all of the use cases into
Paul> one syntax that can be taught in ten minutes.
It unifies all the use cases into *two* syntaxes. The preexisting
%-formatted strings aren't going away anytime soon. They are suitable for
most applications, so new users would have to contend with at least being
able to read, if not write, both forms of string interpolation for the
forseeable future if PEP 215 is adopted.
It hasn't been demonstrated to me that Steve's EvalDict or something similar
couldn't be taught in a similar amount of time. It has the added advantage
that it's essentially the same syntax as the current % syntax. You can use
expressions where before you had to restrict yourself to names. It requires
no change to the language. Just drop it into a module in the std library
and away you go. In fact, coded properly (which Steve is eminently capable
of doing) it would be 100% backward compatible. People running essentially
any version of Python could use it. (I believe Pythonware still makes a 1.4
installer available for Windows.)
Paul> The % syntax is fine for totally different use cases: printf-style
Paul> formatting and interpolation of strings that might be generated at
Paul> runtime.
What do you mean by "totally different"? Most examples I've seen so far
have looked pretty much like
print $"$a $b"
which probably covers about 90% of common usage anyway. The examples in
PEP-215 don't look any more different than an EvalDict-like class could
comfortably handle today either.
--
Skip Montanaro (skip@pobox.com - http://www.mojam.com/)