[Python-Dev] PEP 292, Simpler String Substitutions
Michael McLay
mclay@nist.gov
Fri, 21 Jun 2002 11:57:50 -0400
On Thursday 20 June 2002 06:48 pm, Ka-Ping Yee wrote:
> On Thu, 20 Jun 2002, Oren Tirosh wrote:
> > See http://tothink.com/python/embedpp
>
> Hi Oren,
>
> Your proposal brings up some valid concerns with PEP 215:
>
> 1. run-time vs. compile-time parsing
> 2. how to decide what's an expression
> 3. balanced quoting instead of $
>
I like Oren's PEP as a replacement for PEP 292. But there is one major problem
with his notation. I would change the "`" character to something more
readable. I tried examples with "@", "$", "%", "!", and "?". My preference
was "?", "@", or "$". (The choice should consider the easy of typing on
international keyboards.) The "?" seems like a good choice because the
replacement expresssion will answer the question of what will appear in the
string at that location. Here is Oren's example using the "?" to quote the
expression.
print e"X=?x?, Y=?calc_y(x)?."
The following example is provided for contrast. It has a larger text to
variable substitution ratio.
p = e"""A new character prefix "e" is defined for strings. This prefix
precedes the 'u' and 'r' prefixes, if present. Capital 'E' is also
acceptable. Within an e-string any ?expressions? enclosed in
backquotes are evaluated, converted to strings using the
equivalent of the ?str()? function and embedded in-place into the
In the larger body of text the "?" is clearly visible. I'm not so sure I like
the "?" in the smaller example. It may be because the "?" looks too much like
letters that can appear in a variable name.
The "@" stands out a bit better than "?". This is probably because there are
more pixels turned on and the character is fatter.
print e"X=@x@, Y=@calc_y(x)@."
p = e"""A new character prefix "e" is defined for strings. This prefix
precedes the 'u' and 'r' prefixes, if present. Capital 'E' is also
acceptable. Within an e-string any @expressions@ enclosed in
backquotes are evaluated, converted to strings using the
equivalent of the @str()@ function and embedded in-place into the
e-string."""
The function of the "$" would be recognizable to people migrating from other
languages, but it would be used as a balanced quote, rather than as a
starting character in a variable that will be substituted. (Is this character
easy to type on non-US keyboards? I thought the "$" was one of the character
that is replaced on European keyboards.) If The "@" is available on
international keyboards then I think it would be a better choice.
print e"X=$x$, Y=$calc_y(x)$."
p = e"""A new character prefix "e" is defined for strings. This prefix
precedes the 'u' and 'r' prefixes, if present. Capital 'E' is also
acceptable. Within an e-string any $expressions$ enclosed in
backquotes are evaluated, converted to strings using the
equivalent of the $str()$ function and embedded in-place into the
e-string."""