[Python-Dev] *Simpler* string substitutions

Matthias Urlichs smurf@noris.de
Fri, 21 Jun 2002 22:37:26 +0200


Guido:
>  publishers often turn 'foo' into `foo'

It gets worse. The opposite of ` isn't ' -- it's =B4.
Besides, these are apostrophes and not quotes. _Real_ symmetric=20
quotes are " " or =AB =BB or =93 " or ' ' or =92 ' or ..., but you can'=
t use=20
any of these with just ASCII. Apple's MPW Shell language played with=20
some of these.

Anyway, I agree that real languages use ${} or $WORD and that=20
formatting is best done with ${NAME:format}.

Personally, the "${foo}".sub(foo=3D"bar") syntax (using keyword=20
arguments) looks good and works reasonably well for i18n. A possible=20
simplification would be to use the local+global variables if no=20
arguments are given.

>    def f(x, y):
>        return e"The sum of $x and $y is $(x+y)"

How would that work with i18n?

Proposal: The compiler should translate

	_"foo is ${foo}"

to

	_("foo is ${foo}")

and

	e"foo is ${foo}"

to

	"foo is ${foo}".sub()

>That looks OK to me, especially if it can be combined with u and r to
>  create unicode and raw strings.
>
Exactly.

>  PEP 292 is an attempt to do this *without* involving the parser:
>
>    def f(x, y):
>        return "The sum of $x and $y is $(x+y)".sub()
>
>  Downsides are that it invites using non-literals as formats, with al=
l
>  the security aspects, and that its parsing happens at run-time (no b=
ig
>  deal IMO).
>
You can't do it any other way if you want to use i18nalized strings=20
and formats.

Note that some sentences cannot be internationalized without=20
rearranging some parameters...

--=20
Matthias Urlichs