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

Christian Tismer tismer@tismer.com
Sun, 23 Jun 2002 00:57:52 +0200


Tim Peters wrote:
> [Tim]
> 
>>>Since Christian's reply only increased the apparent contradiction,
>>>allow me to channel: ...
>>
> 
> [Christian Tismer]
> 
>>Huh?
>>Reading from top to bottom, as I used to, I see increasing
>>numbers, which are in the same order as the "increasing hate"
>>(not a linear function, but the same ordering).
>>
>>4 - allowing it to address local/global variables
>>is what I hate the most.
>>This is in no contradiction to allvars(), which is simply
>>a function that puts some variables into a dict, therefore
>>deliberating the interpolation from variable access.
>>
>>Where is the problem, please?
> 
> 
> I was warming up my awesome channeling powers for Guido's impending
> vacation, and all I can figure is that I must have left them parked in
> reverse the last time he came back.  Nothing a 12-pack of Coke didn't cure,
> though!  I channel that you'll graciously accept my apology <wink>.

Whow! A TPA. Will stick it next to my screen :-)

Well, the slightly twisted content of that message shaded
its correct logic, maybe.

Meanwhile, I'd like to drop that hate stuff and replace
it by a little reasoning:

Let's name locals/globals/whatever as "program variables".

If there are program variables directly accessible inside
strings to be interpolated, then I see possible abuse,
if abusers manage to supply such a string in an unforeseen way.
For that reason, I wanted to enforce that an explicit
dictionary has to be passed as an argument, to remind the
programmer that she is responsible for providing access.

But at that time, I wasn't considering compile time string
parsing. Compile time means the strings containing variable
names are evaluated only once, and they behave like constants,
cannot be passed in by a later intruder. That sounds pretty
cool, although I don't see how this fits with I18n, which needs
to change strings at runtime?
Maybe it is possible to parse variable names out, replace
them with some placeholders, and to do the internationalization
after that, still not giving variable access to the final
product.

Example (now also allowing functions):

name1 = "Felix"
age1 = 17
name2 = "Hannes"
age2 = 8

"My little son $name1 is $age1. $name2 is $(age2-age1) years older.".sub()

--> "My little son Felix is 8. Hannes is 9 years older."

This string might be translated under the hood into:
_ipol = {
   x1: name1, x2: age1,
   x3: name2, x4: (age2-age1)
}

"My little son $x1 is $x2. $x3 is $x4 years older.".sub(_ipol)

This string is now safe for further processing.

Maybe the two forms should be syntactically different,
but what I mean is a compile time transformation, that
removes all real variables names in the first place.

interpolation-is-by-value-not-by-name - ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer@tismer.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  pager +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/