[Python-Dev] Yet another string formatting proposal
Oren Tirosh
oren-py-d@hishome.net
Thu, 21 Nov 2002 21:53:23 +0200
On Thu, Nov 21, 2002 at 08:24:54PM +0100, Fredrik Lundh wrote:
> oren won't give up:
>
>
> > "\(a) + \(b) = \(a+b)\n"
> >
> > The expressions embedded in the string are parsed at compile time and
> > any syntax errors in them are detected during compilation.
>
> note that "\(" is commonly used to escape parentheses in regular
> expression strings.
Yes, it might break some existing code that doesn't use proper \\ escaping
or raw strings for regular expression. Note that such code is already
broken in the sense that it uses an undefined escape.
If this turns out to be a real problem a possible alternative is to use
curly braces. There is a precedent for this in u"\N{UNICODE CHAR NAMES}"
Braces are also more visually distinctive and less confusing when the
expression itself contains parentheses:
print "X=\{x}, y=\{calc_y(x)}"
Oren